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 ; Initialize result which is a digit ; Initialize count of result ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code
function countOccurrences ( x , d ) { var count = 0 ; while ( x > 0 ) { if ( x % 10 == d ) count ++ ; x = parseInt ( x / 10 ) ; } return count ; } function maxOccurring ( x ) { if ( x < 0 ) x = - x ; var result = 0 ; var max_count = 1 ; for ( d = 0 ; d <= 9 ; d ++ ) { var count = countOccurrences ( x , d ) ; if ( count >= max_count ) { max_count = count ; result = d ; } } return result ; } var x = 1223355 ; document . write ( " " + maxOccurring ( x ) ) ;