Number of common digits present in two given numbers | Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Input
function CommonDigits ( N , M ) { var count = 0 ; var freq1 = Array ( 10 ) . fill ( 0 ) ; var freq2 = Array ( 10 ) . fill ( 0 ) ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = Math . floor ( N / 10 ) ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = Math . floor ( M / 10 ) ; } var i ; for ( i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } var N = 748294 ; var M = 34298156 ; document . write ( CommonDigits ( N , M ) ) ;