Maximum occurring character in an input string | Set | C ++ implementation to find the maximum occurring character in an input string which is lexicographically first ; function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; to store the maximum occurring character ; length of ' str ' ; get frequency of each character of ' str ' ; for each character , where character is obtained by ( i + ' a ' ) check whether it is the maximum character so far and accodingly update ' result ' ; maximum occurring character ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char getMaxOccurringChar ( char str [ ] ) { int freq [ 26 ] = { 0 } ; int max = -1 ; char result ; int len = strlen ( str ) ; for ( int i = 0 ; i < len ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) if ( max < freq [ i ] ) { max = freq [ i ] ; result = ( char ) ( i + ' a ' ) ; } return result ; } int main ( ) { char str [ ] = " sample ▁ program " ; cout << " Maximum ▁ occurring ▁ character ▁ = ▁ " << getMaxOccurringChar ( str ) ; return 0 ; }