Highest Occuring Character
For a given a string(str), find and return the highest occurring character.
Example:
If there are two characters in the input string with the same frequency, return the character which comes first.
Consider:
Input Format:
Output Format:
Note:
Constraints:
Sample Input 1:
Sample Output 1:
Sample Input 2:
Sample Output 2:
#include <iostream>
#include <cstring>
using namespace std;
#include "solution.h"
char highestOccurringChar(char str[]) {
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[256] = {0};
// Construct character count array from the input
// string.
int len = strlen(str);
int max = 0; // Initialize max count
char result; // Initialize result
// Traversing through the string and maintaining
// the count of each character
for (int i = 0; i < len; i++) {
count[str[i]]++;
}
for (int i = 0; i < len; i++){
if ( count[str[i]]>max ) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
int main() {
int size = 1e6;
char str[size];
cin >> str;
cout << highestOccurringChar(str);
}
Comments
Post a Comment