Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

How to find the maximum occurring character in a given String? using hashmap java

private static void maxOccuringCharacterInString(String s){

        Map<Character,Integer> map = new HashMap<>();
        for(int i=0; i<s.length(); i++){
            if(!map.containsKey(s.charAt(i))){
                map.put(s.charAt(i),1);
            }
            else{
                map.put(s.charAt(i),map.get(s.charAt(i))+1);  // means there is an existing char
                // in string which is already present in map.
            }
        }
        int maxCount = 0;
        char maxChar = 0;
        for(Map.Entry<Character,Integer> m : map.entrySet()) {
            if (m.getValue() > maxCount) {
                maxCount = m.getValue();
                maxChar = m.getKey();
            }
        }
            System.out.println(maxChar+" : "+maxCount);
        }
 
PREVIOUS NEXT
Tagged: #How #find #maximum #occurring #character #hashmap #java
ADD COMMENT
Topic
Name
4+8 =