Skip to content

Instantly share code, notes, and snippets.

@thmain
Created June 1, 2018 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/84046ba8bca7eaa9b5ead763a3066d81 to your computer and use it in GitHub Desktop.
Save thmain/84046ba8bca7eaa9b5ead763a3066d81 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
public class MaximumOccurringCharacter {
static void findMaximumOccurring(String input){
int maxCount =0;
HashMap<Character, Integer> map = new HashMap<>();
char[] chars = input.toCharArray();
for (int i = 0; i <chars.length ; i++) {
char c = chars[i];
if(map.containsKey(c)){
int count = map.get(c);
count++;
if(maxCount<count)
maxCount++;
map.put(c, count);
}else{
map.put(c, 1);
}
}
Set set = map.keySet();
Iterator<Character> iterator = set.iterator();
while(iterator.hasNext()){
char key = iterator.next();
//check count
if(map.get(key)==maxCount){
System.out.println("Character: " + key + " has occurred max times: " + maxCount);
}
}
}
public static void main(String[] args) {
String input = "tutorial horizon";
System.out.println("Input- " + input);
findMaximumOccurring(input);
System.out.println("----------------------");
String input2 = "abcabcdefabcab";
System.out.println("Input- " + input2);
findMaximumOccurring(input2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment