Skip to content
Maximum Occurring Character in a String C++

Maximum Occurring Character in a String

Given, a String of Characters might be a mix of numbers, alphabets, and other special characters and we need to find the maximum occurring character and to do this we need to count the occurrence of each and every character and then the greatest one will be our answer.


Input:Helooo 1234
Output: 'o'

Input:9999 timesss
Output: '9'

Output: '@'

Solution Approach

In this case, everything is unique. We can’t generalize numbers or alphabets together each and every character has it’s own count and we need to add them up.

Note: Here we use ASCII size as 256 unlike 128 as here, we consider all kinds of characters not just numbers or alphabets.


  1. First, we iterate through each and every character in the String
  2. We actually use something similar to a Hashmap
  3. So we create a Hashmap of the size of ASCII [256]
  4. And when we iterate through a character like ‘a’
  5. ASCII value of a is 97
  6. Increment a[97] with 1
  7. Repeat till the end of the String
  8. After every increment, we compare it with MAX
  9. And update MAX value accordingly
  10. Exit loop and print MAX character


Maximum Occurring Character in a String C++

#include <iostream>
#include <string.h>
using namespace std;

char MaxOccuring(char *str)
    int HashCount[256] = {0};
    int length = strlen(str);
    int max = 0;
    char answer;

    for (int i = 0; i < length; i++)
        if (max < HashCount[str[i]])
            max = HashCount[str[i]];
            answer = str[i];
    return answer;

int main()
    char str[] = "AA999####@@@";
    cout <<" Maximum Occurring Character in a String : "
    << MaxOccuring(str);


Maximum Occurring Character in a String : #


I’ve tried to explain the problem in a very simple way, this technique will be useful in many ways, do have a look at HashMap it really helps a lot. This post part of my #30DaysChallenge to create a blog every day on what I learn every day.

Happy Coding – Abhiram Reddy