0
0
CppProgramBeginner · 2 min read

C++ Program to Find Most Frequent Character

Use a frequency array to count characters and find the one with the highest count; for example, int freq[256] = {0}; for (char c : str) freq[(unsigned char)c]++; then find max frequency character.
📋

Examples

Inputhello
OutputMost frequent character: l
Inputaabbccddeeff
OutputMost frequent character: a
Inputxyz
OutputMost frequent character: x
🧠

How to Think About It

To find the most frequent character, count how many times each character appears in the string. Then, look for the character with the highest count. This is like counting votes and picking the winner.
📐

Algorithm

1
Get the input string.
2
Create an array to count frequency of each character.
3
Go through each character and increase its count.
4
Find the character with the highest count.
5
Print that character as the most frequent.
💻

Code

cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    int freq[256] = {0};
    for (char c : str) {
        freq[(unsigned char)c]++;
    }
    int maxFreq = 0;
    char maxChar = '\0';
    for (int i = 0; i < 256; i++) {
        if (freq[i] > maxFreq) {
            maxFreq = freq[i];
            maxChar = (char)i;
        }
    }
    cout << "Most frequent character: " << maxChar << endl;
    return 0;
}
Output
Most frequent character: l
🔍

Dry Run

Let's trace the input 'hello' through the code

1

Initialize frequency array

freq array all zeros

2

Count characters

h: freq[104] = 1, e: freq[101] = 1, l: freq[108] = 2, o: freq[111] = 1

3

Find max frequency

maxFreq = 2, maxChar = 'l'

4

Print result

Output: Most frequent character: l

CharacterASCIIFrequency
h1041
e1011
l1082
o1111
💡

Why This Works

Step 1: Counting characters

We use an array indexed by ASCII values to count how many times each character appears.

Step 2: Finding the max

We scan the frequency array to find the character with the highest count.

Step 3: Output

We print the character with the highest frequency as the most frequent character.

🔄

Alternative Approaches

Using std::map
cpp
#include <iostream>
#include <string>
#include <map>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    map<char, int> freq;
    for (char c : str) {
        freq[c]++;
    }
    char maxChar = '\0';
    int maxFreq = 0;
    for (auto& p : freq) {
        if (p.second > maxFreq) {
            maxFreq = p.second;
            maxChar = p.first;
        }
    }
    cout << "Most frequent character: " << maxChar << endl;
    return 0;
}
Uses map for dynamic character counting, good for Unicode or unknown character sets but slower than array.
Using std::unordered_map
cpp
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;

int main() {
    string str;
    getline(cin, str);
    unordered_map<char, int> freq;
    for (char c : str) {
        freq[c]++;
    }
    char maxChar = '\0';
    int maxFreq = 0;
    for (auto& p : freq) {
        if (p.second > maxFreq) {
            maxFreq = p.second;
            maxChar = p.first;
        }
    }
    cout << "Most frequent character: " << maxChar << endl;
    return 0;
}
Faster average lookup than map, good for large inputs with many different characters.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through the input string once (O(n)) and then loops through a fixed size frequency array (256 elements), which is constant time.

Space Complexity

Uses a fixed size array of 256 integers for frequency counts, so space is O(1) regardless of input size.

Which Approach is Fastest?

Using a fixed size array is fastest for ASCII input; maps add overhead but handle wider character sets.

ApproachTimeSpaceBest For
Array frequencyO(n)O(1)ASCII input, fastest
std::mapO(n log n)O(n)Unicode or unknown chars, ordered keys
std::unordered_mapO(n)O(n)Unicode or unknown chars, faster average
💡
Use an array indexed by character ASCII codes for fast frequency counting when input is ASCII.
⚠️
Forgetting to cast characters to unsigned char when using them as array indices can cause errors.