Skip to content
Related Articles

Related Articles

Find maximum occurring character in a string

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 20 Sep, 2022
View Discussion
Improve Article
Save Article

Given string str. The task is to find the maximum occurring character in the string str.

Examples:

Input: geeksforgeeks
Output: e
Explanation: ‘e’ occurs 4 times in the string

Input: test
Output: t
Explanation: ‘t’ occurs 2 times in the string

Return maximum occurring character in an input string using Hashing:

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 max then update max
  • And update that character in our result variable.

Below is the implementation of the above approach:

C++




// C++ program to output the maximum occurring character
// in a string
#include <bits/stdc++.h>
#define ASCII_SIZE 256
using namespace std;
 
char getMaxOccurringChar(char* str)
{
    // Create array to keep the count of individual
    // characters and initialize the array as 0
    int count[ASCII_SIZE] = { 0 };
 
    // Construct character count array from the input
    // string.
    int len = strlen(str);
    int max = 0; // Initialize max count
    char result; // Initialize result
 
    // Traversing through the string and maintaining
    // the count of each character
    for (int i = 0; i < len; i++) {
        count[str[i]]++;
        if (max < count[str[i]]) {
            max = count[str[i]];
            result = str[i];
        }
    }
 
    return result;
}
 
// Driver program to test the above function
int main()
{
    char str[] = "sample string";
    cout << "Max occurring character is "
         << getMaxOccurringChar(str);
}

Java




// Java program to output the maximum occurring character
// in a string
 
public class GFG {
    static final int ASCII_SIZE = 256;
    static char getMaxOccurringChar(String str)
    {
        // Create array to keep the count of individual
        // characters and initialize the array as 0
        int count[] = new int[ASCII_SIZE];
 
        // Construct character count array from the input
        // string.
        int len = str.length();
        for (int i = 0; i < len; i++)
            count[str.charAt(i)]++;
 
        int max = -1; // Initialize max count
        char result = ' '; // Initialize result
 
        // Traversing through the string and maintaining
        // the count of each character
        for (int i = 0; i < len; i++) {
            if (max < count[str.charAt(i)]) {
                max = count[str.charAt(i)];
                result = str.charAt(i);
            }
        }
 
        return result;
    }
 
    // Driver Method
    public static void main(String[] args)
    {
        String str = "sample string";
        System.out.println("Max occurring character is "
                           + getMaxOccurringChar(str));
    }
}

Python3




# Python program to return the maximum occurring character in the input string
ASCII_SIZE = 256
 
 
def getMaxOccurringChar(str):
    # Create array to keep the count of individual characters
    # Initialize the count array to zero
    count = [0] * ASCII_SIZE
 
    # Utility variables
    max = -1
    c = ''
 
    # Traversing through the string and maintaining the count of
    # each character
    for i in str:
        count[ord(i)] += 1
 
    for i in str:
        if max < count[ord(i)]:
            max = count[ord(i)]
            c = i
 
    return c
 
 
# Driver program to test the above function
str = "sample string"
print("Max occurring character is", getMaxOccurringChar(str))
 
# Although this program can be written in atmost 3 lines in Python
# the above program has been written for a better understanding of
# the reader
 
# Shorter version of the program
# import collections
# str = "sample string"
# print "Max occurring character is " +
#        collections.Counter(str).most_common(1)[0][0]
 
# This code has been contributed by Bhavya Jain

C#




// C# program to output the maximum
// occurring character in a string
using System;
 
class GFG {
    static int ASCII_SIZE = 256;
 
    static char getMaxOccurringChar(String str)
    {
        // Create array to keep the count of
        // individual characters and
        // initialize the array as 0
        int[] count = new int[ASCII_SIZE];
 
        // Construct character count array
        // from the input string.
        int len = str.Length;
        for (int i = 0; i < len; i++)
            count[str[i]]++;
 
        int max = -1; // Initialize max count
        char result = ' '; // Initialize result
 
        // Traversing through the string and
        // maintaining the count of each character
        for (int i = 0; i < len; i++) {
            if (max < count[str[i]]) {
                max = count[str[i]];
                result = str[i];
            }
        }
 
        return result;
    }
 
    // Driver Method
    public static void Main()
    {
        String str = "sample string";
        Console.Write("Max occurring character is "
                      + getMaxOccurringChar(str));
    }
}
 
// This code is contributed by Sam007

PHP




<?php
// PHP program to output the maximum
// occurring character in a string
$ASCII_SIZE = 256;
 
function getMaxOccurringChar($str)
{
    global $ASCII_SIZE;
     
    // Create array to keep the count
    // of individual characters and
    // initialize the array as 0
    $count = array_fill(0, $ASCII_SIZE, NULL);
 
    // Construct character count array
    // from the input string.
    $len = strlen($str);
    $max = 0; // Initialize max count
 
    // Traversing through the string
    // and maintaining the count of
    // each character
    for ($i = 0; $i < ($len); $i++)
    {
        $count[ord($str[$i])]++;
        if ($max < $count[ord($str[$i])])
        {
            $max = $count[ord($str[$i])];
            $result = $str[$i];
        }
    }
 
    return $result;
}
 
// Driver Code
$str = "sample string";
echo "Max occurring character is " .
           getMaxOccurringChar($str);
             
// This code is contributed by ita_c
?>

Javascript




<script>
// Javascript program to output the maximum occurring character
// in a string
    let ASCII_SIZE = 256;
    function getMaxOccurringChar(str)
    {
     
        // Create array to keep the count of individual
        // characters and initialize the array as 0
        let count = new Array(ASCII_SIZE);
        for (let i = 0; i < ASCII_SIZE; i++)
        {
            count[i] = 0;
        }
         
        // Construct character count array from the input
        // string.
        let len = str.length;
        for (let i = 0; i < len; i++)
        {
            count[str[i].charCodeAt(0)] += 1;
        }
        let max = -1;   // Initialize max count
        let result = ' ';   // Initialize result
         
        // Traversing through the string and maintaining
        // the count of each character
        for (let i = 0; i < len; i++)
        {
            if (max < count[str[i].charCodeAt(0)])
            {
                max = count[str[i].charCodeAt(0)];
                result = str[i];
            }
        }
        return result;
    }
     
    // Driver Method
    let str = "sample string";
    document.write("Max occurring character is " , getMaxOccurringChar(str));
     
    // This code is contributed by avanitrachhadiya2155
</script>

Output

Max occurring character is s

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!