Created
June 1, 2018 04:19
-
-
Save thmain/84046ba8bca7eaa9b5ead763a3066d81 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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