Search
 
SCRIPT & CODE EXAMPLE
 

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);
        }
Comment

PREVIOUS NEXT
Code Example
Java :: Stop gninnipS My sdroW! javascrip codewars 
Java :: Java Creating a Java LinkedList 
Java :: linked list vs vector 
Java :: minecraft fabric client message 
Java :: java show my form 
Java :: interface java 
Java :: java class in dataweave 
Java :: bulkwrite mongodb array of objects 
Java :: int[] java 
Java :: fullscreen libgdx 
Java :: singleton implementation 
Java :: convert base64 to pdf object for pdf reader in android studio 
Java :: java synchronized method 
Java :: how to copy an object in java 
Java :: exception handling in java 
Java :: how to code java??????????? 
Java :: spring boot api key authentication example 
Java :: how to convert a arraylist to array in java 
Java :: update in spring boot 
Java :: android studio json parser 
Java :: spring logback configuration 
Java :: copy text from header tag in javacript 
Java :: Could not resolve project :app 
Java :: 2-dimensional Array java 
Java :: Java telegram bot dependency 
Java :: url string from url java 
Java :: getter in java 
Java :: get image file path 
Java :: java data structure 
Java :: how to set the length to int array in java 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =