Strings are a very important topic in programming interviews. It's wise to practice some programming problems focused on strings before your interviews. In this article, you'll learn how to find the most frequently occurring character in a string.

Examples to Sympathise the Problem

Example 1: Let the given string be "Makeuseof". The character 'e' occurs 2 times in the given string and all the other characters occur merely one time. Thus, the character 'due east' has the highest frequency in the given string.

Example 2: Let the given string be "She sees cheese". The graphic symbol 'e' occurs 6 times in the given string and all the other characters occur less than 6 times. Thus, the graphic symbol 'e' has the highest frequency in the given cord.

Approach to Detect the Most Frequently Occurring Character in a String

The hashing technique is the most efficient fashion to find the character having the highest frequency in a string. In this technique, the string is traversed and each character of the string is hashed into an array of ASCII characters.

Permit the input string exist "Makeuseof", each grapheme of this string is hashed as following:

frequency['M'] = i

frequency['a] = ane

frequency['g'] = 1

frequency['e'] = 2

frequency['u'] = i

frequency['s'] = 1

frequency['o'] = 1

frequency['f'] = one

The index of the maximum value in the frequency assortment is returned. Here2 is the highest value, therefore 'due east' is returned.

C++ Program to Notice the Graphic symbol With the Highest Frequency

Below is the C++ programme to discover the character with the highest frequency in a string:

                      // C++ programme to find the character            
// having the highest frequency in a cord
#include <iostream>
#include <string>
#define ASCII_SIZE 256
using namespace std;
char maxFrequencyChar (cord str)
{
// Array to store frequency of each characters
// Initialized the frequency of each graphic symbol as 0
int frequency[ASCII_SIZE] = {0};
// Finding length of the input string
int lenOfStr = str.length();
// Initialize maxFrequency variable
int maxFrequency = -i;
// Initialize maxFrequencyChar variable
char maxFrequencyChar;
// Traversing and maintaining the
// frequency of each characters
for (int i = 0; i < lenOfStr; i++)
{
frequency [str[i]]++;
if (maxFrequency < frequency[str[i]])
{
maxFrequency = frequency[str[i]];
maxFrequencyChar = str[i];
}
}
render maxFrequencyChar;
}
// Commuter Code
int master ()
{
string str1 = "Which witch is which?";
cout < < "str1: " < < str1 < < endl;
cout < < "The highest frequency character is: " < < maxFrequencyChar(str1) < < endl;
cord str2 = "He threw three gratis throws";
cout < < "str2: " < < str2 < < endl;
cout < < "The highest frequency grapheme is: " < < maxFrequencyChar(str2) < < endl;
cord str3 = "Eddie edited information technology";
cout < < "str3: " < < str3 < < endl;
cout < < "The highest frequency graphic symbol is: " < < maxFrequencyChar(str3) < < endl;
string str4 = "Makeuseof";
cout < < "str4: " < < str4 < < endl;
cout < < "The highest frequency character is: " < < maxFrequencyChar(str4) < < endl;
cord str5 = "She sees cheese";
cout < < "str5: " < < str5 < < endl;
cout < < "The highest frequency character is: " < < maxFrequencyChar(str5) < < endl;
}

Output:

          str1: Which witch is            which?
The highest frequency character is: h
str2: He threw three complimentary throws
The highest frequency character is: e
str3: Eddie edited it
The highest frequency character is: d
str4: Makeuseof
The highest frequency character is: e
str5: She sees cheese
The highest frequency character is: eastward

Python Program to Find the Graphic symbol With the Highest Frequency

Below is the Python programme to find the grapheme with the highest frequency in a string:

                      # Python programme to detect the character            
# having the highest frequency in a string
ASCII_SIZE = 256
def maxFrequencyChar (str):
# Assortment to shop frequency of each characters
# Initialized the frequency of each grapheme as 0
frequency = [0] * ASCII_SIZE
# Initialize maxFrequency variable
maxFrequency = -one
# Initialize maxFrequencyChar variable
maxFrequencyChar = ''
# Traversing and maintaining the
# frequency of each characters
for i in str:
frequency[ord(i)] += 1
for i in str:
if maxFrequency < frequency[ord(i)]:
maxFrequency = frequency[ord(i)]
maxFrequencyChar = i
return maxFrequencyChar
# Driver Code
str1 = "Which witch is which?"
impress("str1:", str1)
print("The highest frequency character is:", maxFrequencyChar(str1))
str2 = "He threw 3 free throws"
print("str2:", str2)
impress("The highest frequency graphic symbol is:", maxFrequencyChar(str2))
str3 = "Eddie edited it"
print("str3:", str3)
print("The highest frequency character is:", maxFrequencyChar(str3))
str4 = "Makeuseof"
print("str4:", str4)
impress("The highest frequency character is:", maxFrequencyChar(str4))
str5 = "She sees cheese"
impress("str5:", str5)
print("The highest frequency character is:", maxFrequencyChar(str5))

Output:

          str1: Which witch is            which?
The highest frequency character is: h
str2: He threw three free throws
The highest frequency character is: eastward
str3: Eddie edited it
The highest frequency graphic symbol is: d
str4: Makeuseof
The highest frequency character is: e
str5: She sees cheese
The highest frequency character is: e

C Program to Observe the Grapheme With the Highest Frequency

Below is the C program to find the character with the highest frequency in a string:

                      // C program to observe the grapheme            
// having the highest frequency in a string
#include <iostream>
#include <cstring>
#define ASCII_SIZE 256
using namespace std;
char maxFrequencyChar (char *str)
{
// Array to shop frequency of each characters
// Initialized the frequency of each graphic symbol equally 0
int frequency[ASCII_SIZE] = {0};
// Finding length of the input string
int lenOfStr = strlen(str);
// Initialize maxFrequency variable
int maxFrequency = 0;
// Initialize maxFrequencyChar variable
char maxFrequencyChar;
// Traversing and maintaining the
// frequency of each characters
for (int i = 0; i < lenOfStr; i++)
{
frequency [str[i]]++;
if (maxFrequency < frequency[str[i]])
{
maxFrequency = frequency[str[i]];
maxFrequencyChar = str[i];
}
}
return maxFrequencyChar;
}
// Driver Code
int main ()
{
char str1[] = "Which witch is which?";
printf("str1: %southward", str1);
printf("The highest frequency character is: %c \⁠due north", maxFrequencyChar(str1));
char str2[] = "He threw three costless throws";
printf("str2: %southward", str2);
printf("The highest frequency character is: %c \⁠n", maxFrequencyChar(str2));
char str3[] = "Eddie edited it";
printf("str3: %s", str3);
printf("The highest frequency character is: %c \⁠n", maxFrequencyChar(str3));
char str4[] = "Makeuseof";
printf("str4: %due south", str4);
printf("The highest frequency graphic symbol is: %c \⁠n", maxFrequencyChar(str4));
char str5[] = "She sees cheese";
printf("str1: %due south", str5);
printf("The highest frequency graphic symbol is: %c \⁠n", maxFrequencyChar(str5));
}

Output:

          str1: Which witch is            which?
The highest frequency graphic symbol is: h
str2: He threw three costless throws
The highest frequency grapheme is: eastward
str3: Eddie edited it
The highest frequency character is: d
str4: Makeuseof
The highest frequency character is: e
str5: She sees cheese
The highest frequency grapheme is: due east

JavaScript Program to Find the Character With the Highest Frequency

Below is the JavaScript program to find the character with the highest frequency in a cord:

                      // JavaScript program to notice the grapheme            
// having the highest frequency in a string
let ASCII_SIZE = 256;
part maxFrequencyChar(str)
{
// Array to store frequency of each characters
// Initialized the frequency of each graphic symbol equally 0
permit frequency = new Array(ASCII_SIZE);
for (let i = 0; i < ASCII_SIZE; i++)
{
frequency[i] = 0;
}
// Finding length of the input string
let lenOfStr = str.length;
for (permit i = 0; i < lenOfStr; i++)
{
frequency[str[i].charCodeAt(0)] += 1;
}
// Initialize maxFrequency variable
let maxFrequency = -1;
// Initialize maxFrequencyChar variable
permit maxFrequencyChar = '';
// Traversing and maintaining the
// frequency of each characters
for (let i = 0; i < lenOfStr; i++)
{
if (maxFrequency &lt; frequency [str[i] .charCodeAt(0)])
{
maxFrequency = frequency[str[i].charCodeAt(0)];
maxFrequencyChar = str[i];
}
}
return maxFrequencyChar;
}
// Driver Code
let str1 = "Which witch is which?";
certificate.write("str1: " + str1 + "<br>");
certificate.write("The highest frequency character is: " + maxFrequencyChar(str1) + "<br>")
let str2 = "He threw three free throws";
document.write("str2: " + str2 + "<br>");
document.write("The highest frequency graphic symbol is: " + maxFrequencyChar(str2) + "<br>")
let str3 = "Eddie edited it";
document.write("str3: " + str3 + "<br>");
certificate.write("The highest frequency character is: " + maxFrequencyChar(str3) + "<br>")
let str4 = "Makeuseof";
document.write("str4: " + str4 + "<br>");
certificate.write("The highest frequency character is: " + maxFrequencyChar(str4) + "<br>")
let str5 = "She sees cheese";
document.write("str5: " + str5 + "<br>");
document.write("The highest frequency character is: " + maxFrequencyChar(str5) + "<br>")

Output:

          str1: Which witch is            which?
The highest frequency character is: h
str2: He threw three free throws
The highest frequency graphic symbol is: east
str3: Eddie edited information technology
The highest frequency character is: d
str4: Makeuseof
The highest frequency grapheme is: e
str5: She sees cheese
The highest frequency character is: e

Analyze the Fourth dimension and Space Complexity

The fourth dimension complexity of themaxFrequencyChar() function is O(due north). The space complexity of themaxFrequencyChar() office is O(one) as a stock-still space (Hash array). Information technology does not depend on the input string size.

Big-O notation gives you a way to calculate how long information technology will accept to run your lawmaking. It's one of the nigh important concepts for the analysis of algorithms. If you lot're a programmer, you must know about Big-O Notation.

Read Next