Question { Nagarro, 15545 }
A string of charaters were given. Find the highest
occurance of a character and display that character.
eg.: INPUT: AEGBCNAVNEETGUPTAEDAGPE
Answer
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class TestClass1 {
public static void main(String[] args) {
HashMap
hm = new HashMap
Integer>();
String str = "AABBBCCZX";
if (str != null) {
char[] charArray = str.toCharArray();
for (char ch : charArray) {
int counter = 0;
for (int j = 0; j < charArray.length; j++) {
if (ch == charArray[j]) {
counter = counter + 1;
}
}
hm.put(ch, counter);
}
Set s = hm.keySet();
Iterator itr = s.iterator();
while (itr.hasNext()) {
char key = (char) itr.next();
System.out.println("Character : " + key + " Occurence : "
+ hm.get(key));
}
}
}
}