Find most Frequent Character in String

Find most Frequent Character in String” is a problem of string data structure. Here, we are given a string of length ‘n’ and our task is to write a program to find the most frequent character of the string. 

If there are more than one character which have same highest frequency, print the one which appears initially.

Example (Find most Frequent Character in String):

INPUT:
HELPMESTUDYBRO
OUTPUT:
E is most frequent character present in the string

There might be multiple ways to find most frequent character in string. Some of them are:

METHOD 1: Brute – force Approach to find most frequent Character in String

The simplest solution to find the most frequent character of the string is to count frequency of each character of the string and then print the character with highest frequency.

C++ Program to find most frequent character in string is as follows:

/* Program to find most frequent character in string */
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    string str;  
    /* Scan the String */  
    cout<<"Enter a string:\n";  
    getline(cin,str);  
      
    /* Initialize basic variables */  
    int max = 0;  
    char res;  
    int size = str.length();  
    for(int i = 0; i < size; i++)  
    {  
        int count = 0;  
        /* counting frequency of each character */  
        for(int j = 0; j < size; j++)  
        {  
            if(str[j] == str[i])  
            {  
                count++;  
            }  
        }  
        if(count >  max)  
        {  
            max = count;  
            res = str[i];  
        }  
    }  
    cout<<"The most frequent character of the string is "<<res;  
}  

OUTPUT:
Enter a string: 
helpmestudybro
The most frequent character of the string is e
Time Complexity:
O(n^2), where n is the length of the string.

METHOD 2: Hashing Based Approach to find most frequent character in string

The steps required to find most frequent character in string are as follows:

  1. Create an array of size 256 and initialize with 0. (we have a character set of 256 characters ;ASCII: 0-255, we create a hash to store the frequency of each character. Initially, each character has 0 frequency).
  2. Scan the string and increment array[str[i]] with 1.
  3. Finally, scan the string again and find and compare the maximum corresponding value present in the hash array for each character. Store the index value at local variable.
  4. Print the character with ASCII value to that of local variable.

C++ Program to find most frequent character in string is as follows:

/* Program to find most frequent character in string */
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    string str;  
    /* Scan the String */  
    cout<<"Enter a string:\n";  
    getline(cin,str);  
      
    /* Create a HashMap */  
    int hash[256] = {0};  
      
    /* Scan the string and count frequency  
       of each character of the string */  
    for(int i = 0; i < str.length(); i++)  
    {  
        hash[str[i]]++;  
    }  
     
    /* Temporary Variable to Store Result */ 
    int res = 0;  
    char ch;  
      
    /* Compare and find the most frequent character in string */  
    for(int i = 0; i <  str.length(); i++)  
    {  
        if(hash[str[i]] > res)  
        {  
            res = hash[str[i]];  
            ch = str[i];  
        }  
    }  
      
    /* Print the result */  
    cout<<"The most frequent character of the string is "<<ch;  
         
}  

OUTPUT:
Enter a string: 
helpmestudybro
The most frequent character of the string is e
Time Complexity: 
O(n), where n is the length of the string.

Related Posts:

  1. Check Anagram Strings.
  2. Check Whether Given String is Palindromic Anagram or Not.
  3. Check Whether Given String is Panagram or Not.
  4. Find First Non-Repeating Character of the String.
  5. Find Most Frequent Element of the Array.
  6. Find Pair in an Array with Given Sum.
  7. Find First Repeating Element of an Array.
  8. Find Majority Element of an Array.
  9. Program to Check whether given String is Palindrome or not.
  10. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  11. Program to count total number of words in a string.
  12. Program to find smallest and largest word of the String.
  13. Program to find Most Frequent Character of the String.
  14. Program to Remove all the blank spaces from the String.
  15. Program to check if String is isogram or not.
  16. Program to Reverse Each word of the String.
  17. Program to Print All the Substring of the Given String.
  18. Program to find longest palindromic Substring.
  19. Program to check Anagram Strings.
  20. Program to check whether two Strings are Rotation of each other or not.

You may also like...