package interview;
//count no. of duplicate character occurance
import java.util.*;
//steps:
//string --->char array---->(convert to lowercase)---->hashmap
public class DuplicateChar {
public static void main(String[] args)
{
String str ="programming world445";
char[] ch = str.toCharArray();
char[] upper_ch = convertLower(ch);
System.out.println(upper_ch);
Map<Character, Integer> map = new HashMap<>();
for(char c : upper_ch)
{
if(map.containsKey(c))
{
map.put(c, map.get(c)+1);
}
else
{
map.put(c, 1);
}
}
//****************
for(Map.Entry<Character,Integer>entry : map.entrySet())
{
if(entry.getValue()>1)
{
System.out.println(entry.getKey()+"-->"+entry.getValue());
}
}
}
//***********************************
private static char[] convertLower(char[] ch)
{
for(int i=0;i<ch.length;i++)
{
if(Character.isLetter(ch[i])&& (Character.isLowerCase(ch[i])))
{
ch[i]= Character.toUpperCase(ch[i]);
}
}
return ch;
}
}
//count no. of duplicate character occurance
import java.util.*;
//steps:
//string --->char array---->(convert to lowercase)---->hashmap
public class DuplicateChar {
public static void main(String[] args)
{
String str ="programming world445";
char[] ch = str.toCharArray();
char[] upper_ch = convertLower(ch);
System.out.println(upper_ch);
Map<Character, Integer> map = new HashMap<>();
for(char c : upper_ch)
{
if(map.containsKey(c))
{
map.put(c, map.get(c)+1);
}
else
{
map.put(c, 1);
}
}
//****************
for(Map.Entry<Character,Integer>entry : map.entrySet())
{
if(entry.getValue()>1)
{
System.out.println(entry.getKey()+"-->"+entry.getValue());
}
}
}
//***********************************
private static char[] convertLower(char[] ch)
{
for(int i=0;i<ch.length;i++)
{
if(Character.isLetter(ch[i])&& (Character.isLowerCase(ch[i])))
{
ch[i]= Character.toUpperCase(ch[i]);
}
}
return ch;
}
}
No comments:
Post a Comment