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 ; Driver Code ; Input
def CommonDigits ( N , M ) : NEW_LINE INDENT count = 0 NEW_LINE freq1 = [ 0 ] * 10 NEW_LINE freq2 = [ 0 ] * 10 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT freq1 [ N % 10 ] += 1 NEW_LINE N = N // 10 NEW_LINE DEDENT while ( M > 0 ) : NEW_LINE INDENT freq2 [ M % 10 ] += 1 NEW_LINE M = M // 10 NEW_LINE DEDENT for i in range ( 10 ) : NEW_LINE INDENT if ( freq1 [ i ] > 0 and freq2 [ i ] > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 748294 NEW_LINE M = 34298156 NEW_LINE print ( CommonDigits ( N , M ) ) NEW_LINE DEDENT