Return maximum occurring character in an input string | PHP program to output the maximum occurring character in a string ; Create array to keep the count of individual characters and initialize the array as 0 ; Construct character count array from the input string . ; Initialize max count ; Traversing through the string and maintaining the count of each character ; Driver Code
< ? php $ ASCII_SIZE = 256 ; function getMaxOccuringChar ( $ str ) { global $ ASCII_SIZE ; $ count = array_fill ( 0 , $ ASCII_SIZE , NULL ) ; $ len = strlen ( $ str ) ; $ max = 0 ; for ( $ i = 0 ; $ i < ( $ len ) ; $ i ++ ) { $ count [ ord ( $ str [ $ i ] ) ] ++ ; if ( $ max < $ count [ ord ( $ str [ $ i ] ) ] ) { $ max = $ count [ ord ( $ str [ $ i ] ) ] ; $ result = $ str [ $ i ] ; } } return $ result ; } $ str = " sample ▁ string " ; echo " Max ▁ occurring ▁ character ▁ is ▁ " . getMaxOccuringChar ( $ str ) ; ? >