VOOZH about

URL: https://www.geeksforgeeks.org/dsa/return-maximum-occurring-character-in-the-input-string-3/

⇱ Find maximum occurring character in a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find maximum occurring character in a string

Last Updated : 31 Jan, 2026

Given string s, we need is to find the maximum occurring character in the string str.

Examples:

Input: s="geeksforgeeks"
Output: 'e'
Explanation: 'e' occurs 4 times in the string

Input: s="test"
Output: 't'
Explanation: 't' occurs 2 times in the string

In this approach we simply use the unordered_map from STL to store the frequency of every character and while adding characters to map we take a variable count to determine the element having highest frequency.

Implementation :


Output
Max occurring character is: s


The idea is to store the frequency of every character in the array and return the character with maximum count.

Follow the steps to solve the problem:

  • Create a count array of size 256 to store the frequency of every character of the string
  • Maintain a max variable to store the maximum frequency so far whenever encounter a frequency more than the max then update the max
  • And update that character in our result variable.

Below is the implementation of the above approach:


Output
Character: e, Count: 4

Time Complexity: O(N), Traversing the string of length N one time.
Auxiliary Space: O(1)

Comment
Article Tags: