How to Find the Most Frequently Occurring Character in a String

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 Understand 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 only once. Thus, the character ‘e’ has the highest frequency in the given string.

Example 2: Let the given string be “She sees cheese”. The character ‘e’ occurs 6 times in the given string and all the other characters occur less than 6 times. Thus, the character ‘e’ has the highest frequency in the given string.

Approach to Find the Most Frequently Occurring Character in a String

The hashing technique is the most efficient way 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.

Let the input string be “Makeuseof”, each character of this string is hashed as following:

frequency[‘M’] = 1

frequency[‘a] = 1

frequency[‘k’] = 1

frequency[‘e’] = 2

frequency[‘u’] = 1

frequency[‘s’] = 1

frequency[‘o’] = 1

frequency[‘f’] = 1

The index of the maximum value in the frequency array is returned. Here 2 is the highest value, therefore ‘e’ is returned.

C++ Program to Find the Character With the Highest Frequency

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

Related: How to Count the Occurrences of a Given Character in a String

// C++ program to find the character
// having the highest frequency in a string
#include
#include
#define ASCII_SIZE 256
using namespace std;
char maxFrequencyChar(string str)
{
// Array to store frequency of each characters
// Initialized the frequency of each character as 0
int frequency[ASCII_SIZE] = {0};
// Finding length of the input string
int lenOfStr = str.length();
// Initialize maxFrequency variable
int maxFrequency = -1;
// Initialize maxFrequencyChar variable
char maxFrequencyChar;
// Traversing and maintaining the
// frequency of each characters
for (int i = 0; i {
frequency[str[i]]++;
if (maxFrequency {
maxFrequency = frequency[str[i]];
maxFrequencyChar = str[i];
}
}
return maxFrequencyChar;
}
// Driver Code
int main()
{
string str1 = "Which witch is which?";
cout cout string str2 = "He threw three free throws";
cout cout string str3 = "Eddie edited it";
cout cout string str4 = "Makeuseof";
cout cout string str5 = "She sees cheese";
cout cout }

Output:

str1: Which witch is which?
The highest frequency character is: h
str2: He threw three free 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: e

Python Program to Find the Character With the Highest Frequency

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

Related: How to Reverse a String in C++, Python, and JavaScript

# Python program to find the character
# having the highest frequency in a string
ASCII_SIZE = 256
def maxFrequencyChar(str):
# Array to store frequency of each characters
# Initialized the frequency of each character as 0
frequency = [0] * ASCII_SIZE
# Initialize maxFrequency variable
maxFrequency = -1
# 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 maxFrequency = frequency[ord(i)]
maxFrequencyChar = i
return maxFrequencyChar
# Driver Code
str1 = "Which witch is which?"
print("str1:", str1)
print("The highest frequency character is:", maxFrequencyChar(str1))
str2 = "He threw three free throws"
print("str2:", str2)
print("The highest frequency character is:", maxFrequencyChar(str2))
str3 = "Eddie edited it"
print("str3:", str3)
print("The highest frequency character is:", maxFrequencyChar(str3))
str4 = "Makeuseof"
print("str4:", str4)
print("The highest frequency character is:", maxFrequencyChar(str4))
str5 = "She sees cheese"
print("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: 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: e

C Program to Find the Character With the Highest Frequency

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

Related: How to Find Vowels, Consonants, Digits, and Special Characters in a String

// C program to find the character
// having the highest frequency in a string
#include
#include
#define ASCII_SIZE 256
using namespace std;
char maxFrequencyChar(char *str)
{
// Array to store frequency of each characters
// Initialized the frequency of each character as 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 {
frequency[str[i]]++;
if (maxFrequency {
maxFrequency = frequency[str[i]];
maxFrequencyChar = str[i];
}
}
return maxFrequencyChar;
}
// Driver Code
int main()
{
char str1[] = "Which witch is which?";
printf("str1: %s", str1);
printf("The highest frequency character is: %c ⁠n", maxFrequencyChar(str1));
char str2[] = "He threw three free throws";
printf("str2: %s", 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: %s", str4);
printf("The highest frequency character is: %c ⁠n", maxFrequencyChar(str4));
char str5[] = "She sees cheese";
printf("str1: %s", str5);
printf("The highest frequency character is: %c ⁠n", 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: 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: e

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 string:

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

Analyze the Time and Space Complexity

The time complexity of the maxFrequencyChar() function is O(n). The space complexity of the maxFrequencyChar() function is O(1) as a fixed space (Hash array). It does not depend on the input string size.

Big-O notation gives you a way to calculate how long it will take to run your code. It’s one of the most important concepts for the analysis of algorithms. If you’re a programmer, you must know about Big-O Notation.

Source: makeuseof.com

Related posts

Connections #345: Today’s Answer and Clues (Tuesday, May 21, 2024)

Take Better Smartphone Photos by Unlearning These Bad Habits

Why Buying Instagram Followers Is a Terrible Mistake