Maximum occurring character in an input string | Set | function to find the maximum occurring character in an input string which is lexicographically first ; freq [ ] used as hash table ; to store maximum frequency ; 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
def getMaxOccurringChar ( str ) : NEW_LINE INDENT freq = [ 0 for i in range ( 100 ) ] NEW_LINE max = - 1 NEW_LINE len__ = len ( str ) NEW_LINE DEDENT ' NEW_LINE INDENT for i in range ( 0 , len__ , 1 ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT ' NEW_LINE INDENT for i in range ( 26 ) : NEW_LINE INDENT if ( max < freq [ i ] ) : NEW_LINE INDENT max = freq [ i ] NEW_LINE result = chr ( ord ( ' a ' ) + i ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " sample ▁ program " NEW_LINE print ( " Maximum ▁ occurring ▁ character ▁ = " , getMaxOccurringChar ( str ) ) NEW_LINE DEDENT