Find the most frequent digit without using array / string | Simple function to count occurrences of digit d in x ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code
< ? php function countOccurrences ( $ x , $ d ) { $ count = 0 ; while ( $ x ) { if ( $ x % 10 == $ d ) $ count ++ ; $ x = ( int ) ( $ x / 10 ) ; } return $ count ; } function maxOccurring ( $ x ) { if ( $ x < 0 ) $ x = - $ x ; for ( $ d = 0 ; $ d <= 9 ; $ d ++ ) { $ count = countOccurrences ( $ x , $ d ) ; if ( $ count >= $ max_count ) { $ max_count = $ count ; $ result = $ d ; } } return $ result ; } $ x = 1223355 ; echo " Max ▁ occurring ▁ digit ▁ is ▁ " . maxOccurring ( $ x ) ; ? >