Number of common digits present in two given numbers

22 Apr, 2021
Given two positive numbers N and M, the task is to count the number of digits that are present in both N and M.
Examples:
Input: N = 748294, M = 34298156Output: 4Explanation: The digits that are present in both the numbers are {4, 8, 2, 9}. Therefore, the required count is 4.
Input: N = 111222, M = 333444Output: 0Explanation: No common digits present in the two given numbers.
Approach: The given problem can be solved using Hashing. Follow the steps below to solve the problem:
Initialize a variable, say count as 0, to store the number of digits that are common in both the numbers.
Initialize two arrays, say freq1[10] and freq2[10] as {0}, to store the count of digits present in the integers N and M respectively.
Iterate over the digits of the integer N and increment the count of each digit in freq1[] by 1.
Iterate over the digits of the integer M and increment the count of each digit in freq2[] by 1.
Iterate over the range [0, 9] and increment the count by 1 if freq1[i] and freq2[i] both exceeds 0.
Finally, after completing the above steps, print the count obtained as the required answer.
Below is the implementation of the above approach:
C++14
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to count number of digits// that are common in both N and Mint CommonDigits(int N, int M){    // Stores the count of common digits    int count = 0;     // Stores the count of digits of N    int freq1[10] = { 0 };     // Stores the count of digits of M    int freq2[10] = { 0 };     // Iterate over the digits of N    while (N > 0) {         // Increment the count of        // last digit of N        freq1[N % 10]++;         // Update N        N = N / 10;    }    // Iterate over the digits of M    while (M > 0) {         // Increment the count of        // last digit of M        freq2[M % 10]++;         // Update M        M = M / 10;    }    // Iterate over the range [0, 9]    for (int i = 0; i < 10; i++) {         // If freq1[i] and freq2[i] both exceeds 0        if (freq1[i] > 0 & freq2[i] > 0) {             // Increment count by 1            count++;        }    }     // Return the count    return count;} // Driver Codeint main(){    // Input    int N = 748294;    int M = 34298156;     cout << CommonDigits(N, M);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{ // Function to count number of digits// that are common in both N and Mstatic int CommonDigits(int N, int M){         // Stores the count of common digits    int count = 0;     // Stores the count of digits of N    int freq1[] = new int[10];     // Stores the count of digits of M    int freq2[] = new int[10];     // Iterate over the digits of N    while (N > 0)    {                 // Increment the count of        // last digit of N        freq1[N % 10]++;         // Update N        N = N / 10;    }         // Iterate over the digits of M    while (M > 0)    {                 // Increment the count of        // last digit of M        freq2[M % 10]++;         // Update M        M = M / 10;    }         // Iterate over the range [0, 9]    for(int i = 0; i < 10; i++)    {                 // If freq1[i] and freq2[i] both exceeds 0        if (freq1[i] > 0 & freq2[i] > 0)        {                         // Increment count by 1            count++;        }    }     // Return the count    return count;} // Driver Codepublic static void main(String[] args){         // Input    int N = 748294;    int M = 34298156;     System.out.print(CommonDigits(N, M));}} // This code is contributed by gauravrajput1
# Python3 program for the above approach # Function to count number of digits# that are common in both N and Mdef CommonDigits(N, M):         # Stores the count of common digits    count = 0     # Stores the count of digits of N    freq1 = [0] * 10     # Stores the count of digits of M    freq2 = [0] * 10     # Iterate over the digits of N    while (N > 0):                 # Increment the count of        # last digit of N        freq1[N % 10] += 1         # Update N        N = N // 10             # Iterate over the digits of M    while (M > 0):                 # Increment the count of        # last digit of M        freq2[M % 10] += 1         # Update M        M = M // 10     # Iterate over the range [0, 9]    for i in range(10):                 # If freq1[i] and freq2[i] both exceeds 0        if (freq1[i] > 0 and freq2[i] > 0):                         # Increment count by 1            count += 1     # Return the count    return count # Driver Codeif __name__ == '__main__':         # Input    N = 748294    M = 34298156     print (CommonDigits(N, M)) # This code is contributed by mohit kumar 29
// C# program for the above approachusing System; class GFG{     // Function to count number of digits// that are common in both N and Mstatic int CommonDigits(int N, int M){         // Stores the count of common digits    int count = 0;     // Stores the count of digits of N    int[] freq1 = new int[10];     // Stores the count of digits of M    int[] freq2 = new int[10];     // Iterate over the digits of N    while (N > 0)    {                 // Increment the count of        // last digit of N        freq1[N % 10]++;         // Update N        N = N / 10;    }         // Iterate over the digits of M    while (M > 0)    {                 // Increment the count of        // last digit of M        freq2[M % 10]++;         // Update M        M = M / 10;    }         // Iterate over the range [0, 9]    for(int i = 0; i < 10; i++)    {                 // If freq1[i] and freq2[i]        // both exceeds 0        if (freq1[i] > 0 & freq2[i] > 0)        {                         // Increment count by 1            count++;        }    }     // Return the count    return count;} // Driver codestatic void Main(){         // Input    int N = 748294;    int M = 34298156;     Console.WriteLine(CommonDigits(N, M));}} // This code is contributed by sanjoy_62
<script> // javascript program for the above approach // Function to count number of digits// that are common in both N and Mfunction CommonDigits(N,M){    // Stores the count of common digits    var count = 0;     // Stores the count of digits of N    var freq1 = Array(10).fill(0);     // Stores the count of digits of M    var freq2 = Array(10).fill(0);     // Iterate over the digits of N    while (N > 0) {         // Increment the count of        // last digit of N        freq1[N % 10]++;         // Update N        N = Math.floor(N / 10);    }    // Iterate over the digits of M    while (M > 0) {         // Increment the count of        // last digit of M        freq2[M % 10]++;         // Update M        M = Math.floor(M / 10);    }    var i;    // Iterate over the range [0, 9]    for (i = 0; i < 10; i++) {         // If freq1[i] and freq2[i] both exceeds 0        if (freq1[i] > 0 & freq2[i] > 0) {             // Increment count by 1            count++;        }    }     // Return the count    return count;} // Driver Code     // Input    var N = 748294;    var M = 34298156;     document.write(CommonDigits(N, M)); </script>
4
 
Time Complexity: O(digits(N)+digits(M))Auxiliary Space: O(10)
 
mohit kumar 29
GauravRajput1
sanjoy_62
SURENDRA_GANGWAR
frequency-counting
number-digits
Hash
School Programming
Searching
Searching
Hash
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
What is Hashing | A Complete Tutorial
Real-time application of Data Structures
Find k numbers with most occurrences in the given array
Find the length of largest subarray with 0 sum
Non-Repeating Element
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java