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
using System ; class GFG { static char getMaxOccurringChar ( string str ) { int [ ] freq = new int [ 26 ] ; int max = - 1 ; char result = ( char ) 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ 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 ; } public static void Main ( ) { string str = " sample ▁ program " ; Console . WriteLine ( " Maximum ▁ occurring ▁ character ▁ = ▁ " + getMaxOccurringChar ( str ) ) ; } }