Maximum Occuring Character | Practice | GeeksforGeeks

Given a string str. The task is to find the maximum occurring character in the string str. If more than one character occurs the maximum number of time then print the lexicographically smaller character.
Example 1:
Input:
str = testsample
Output: e
Explanation: e is the character which
is having the highest frequency.
Example 2:
Input:
str = output
Output: t
Explanation:  t and u are the characters
with the same frequency, but t is
lexicographically smaller.
Your Task:
The task is to complete the function getMaxOccuringChar() which returns the character which is most occurring.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of distinct characters).
Note: N = |s|
Constraints:
1 ≤ |s| ≤ 100
0
singhvipnesh61 day ago
class Solution{   //Function to find the maximum occurring character in a string.   public static char getMaxOccuringChar(String line)   {       char []ch=line.toCharArray();       Arrays.sort(ch);       HashMap<Character,Integer>map=new HashMap<>();       for(int i=0;i<ch.length;i++)       {           map.put(ch[i],map.getOrDefault(ch[i],0)+1);       }       int max=-1;       char c='*';        for(int i=0;i<ch.length;i++)       {           int get=map.get(ch[i]);           if(get>max)           {               max=get;               c=ch[i];           }       }       return c;   }   }
+1
aniket6518gadhe3 days ago
JAVA SOLUTION (lowest time complexity)
Total Time Taken:
0.1/1.14
 
public static char getMaxOccuringChar(String s)   {       int max=0;       char result=Character.MIN_VALUE;       HashMap<Character,Integer> map=new HashMap<>();       for(int i=0;i<s.length();i++){           char ch= s.charAt(i);           map.put(ch, map.getOrDefault(ch, 0)+1);           if(map.get(ch)>max){               result=ch;               max=map.get(ch);           }           if(map.get(ch)==max && ch<result)               result=ch;       }       return result;   }
0
deeplakhotia8761 week ago
Simple CPP Solution using Map
 
char getMaxOccuringChar(string str)   {       map<char,int>m;       for(int i=0;i<str.length();i++){           m[str[i]]++;       }       int x=0;       char ch;       for(auto it=m.begin();it!=m.end();it++){           if(it->second>x){               ch=it->first;               x=it->second;           }       }       return ch;   }
 
 
Upvote if it helped you.
0
mayank180919991 week ago
   char getMaxOccuringChar(string str)
    {
        // Your code here
        map<char,int>m;
        sort(str.begin(),str.end());
        for(int i=0;i<str.length();i++){
            m[str[i]]++;
        }
        char ans;
        int max=INT_MIN;
        for(auto it:m){
            if(it.second>max){
                max=it.second;
                ans=it.first;
            }
        }
     return ans;
    }
0
ajay256412 weeks ago
Simple java code using HashMap
 public static char getMaxOccuringChar(String line)
    {   
        int n=line.length();
        Map<Character,Integer> hm=new HashMap<>();
        char ch = line.charAt(0);
        int max=1;
        for(int i=0;i<n;i++)
            hm.merge(line.charAt(i),1,Integer::sum);//if key //does not exit insert it with value 1 and if key already //exist then sum the value with 1.
        for(Map.Entry<Character,Integer>entry:hm.entrySet()){
            if(entry.getValue()>max){
                max=entry.getValue();
                ch=entry.getKey();
            }
            else if(entry.getValue()==max)
                ch=ch<entry.getKey()?ch:entry.getKey();
        }
        return ch;
    }
0
sauravskit7492 weeks ago
 char getMaxOccuringChar(string str)   {       // Your code here       map<char,int> m;       char b;       for(int i=0;i<str.size();i++){           m[str[i]]++;       }       int i=INT_MIN;       for(auto x:m){                      if(x.second>i){               b=x.first;               i=x.second;           }       }       return b;   } 
0
swapniltayal4223 weeks ago
class Solution:   #Function to find the maximum occurring character in a string.   def getMaxOccurringChar(self,s):       #code here       d = dict()       for i in s:           d[i] = d.get(i, 0) + 1       m = -1       q = 'z'       for k, v in d.items():           if (v > m):               q = 'z'               m = v               ans = min(q, k)           if (v == m):               ans = min(q, k)               q = ans       return ans
0
vishal65p4 weeks ago
char getMaxOccuringChar(string str)   {       // Your code here       int arr[26] = {0};              for(int i=0; i<str.length(); i++){           char ch = str[i];                      int number =0;           if(ch>='a' && ch<='z'){               number = ch - 'a';           }           else{               number = ch-'A';           }           arr[number]++;       }              int maxi = -1;       int ans = 0;       for(int i =0; i<26; i++){           if(maxi< arr[i]){               ans = i;               maxi = arr[i];           }       }       char finalAns = 'a' + ans;              return finalAns;   }
0
vishal65p
This comment was deleted.
0
mohammadtanveer75401 month ago
class Solution
{
    public:
    char getMaxOccuringChar(string str)
    {
        int char_count[26]={0};
        for(int i=0;i<str.size();i++)
            char_count[str[i]-'a']++;
        int max=-1, max_index=-1;
        for(int i=0;i<26;i++)
            if(char_count[i]>max){
                max=char_count[i];
                max_index=i;
            }
        
        char c = char(max_index+97);
        return c;
    }

};
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.