Maximum Frequency of the characters in an array

Two things are needed here:

1. charCount array to get the frequency of all characters. Click here to learn about charCount[].

2. maxCount variable to get the maximum frequency char.

Click here to debug this program.

#include <iostream>
#include <vector>

using namespace std;

int maxFreqofChars(string s, char *ch)
{
   int N = s.length();
   int maxCount = 0;
   vector<int> charCount(26);
   for (int i = 0; i < N; i++) {
       //cout << s[i] - 'A' << " stores " << s[i] << endl;
       charCount[s[i] -'A']++;
       if (maxCount < charCount[s[i] - 'A']) {
           maxCount = charCount[s[i] - 'A'];
           *ch = s[i];
       }
   }
   return maxCount;
}


int main()
{
    //string s = "AABABBA";
    string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZK";
    char ch = ' ';
    int freq = maxFreqofChars(s, &ch);
    cout << "The maximum frequency of the char ["  << ch << "] in an array "  << s <<  " is " << freq << endl;
    return 0;
}


Output:

1
The maximum frequency of the char [K] in an array ABCDEFGHIJKLMNOPQRSTUVWXYZK is 2


Click here to find the maximum frequency of the digits.

Comments