Skip to content
Related Articles
Number of common digits present in two given numbers
• Difficulty Level : Medium
• Last Updated : 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 = 34298156
Output: 4
Explanation: The digits that are present in both the numbers are {4, 8, 2, 9}. Therefore, the required count is 4.

Input: N = 111222, M = 333444
Output: 0
Explanation: 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 and freq2 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

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to count number of digits``// that are common in both N and M``int` `CommonDigits(``int` `N, ``int` `M)``{``    ``// Stores the count of common digits``    ``int` `count = 0;` `    ``// Stores the count of digits of N``    ``int` `freq1 = { 0 };` `    ``// Stores the count of digits of M``    ``int` `freq2 = { 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 Code``int` `main()``{``    ``// Input``    ``int` `N = 748294;``    ``int` `M = 34298156;` `    ``cout << CommonDigits(N, M);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `// Function to count number of digits``// that are common in both N and M``static` `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 Code``public` `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

 `# Python3 program for the above approach` `# Function to count number of digits``# that are common in both N and M``def` `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 Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Input``    ``N ``=` `748294``    ``M ``=` `34298156` `    ``print` `(CommonDigits(N, M))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to count number of digits``// that are common in both N and M``static` `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``;` `    ``// Stores the count of digits of M``    ``int``[] freq2 = ``new` `int``;` `    ``// 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 code``static` `void` `Main()``{``    ` `    ``// Input``    ``int` `N = 748294;``    ``int` `M = 34298156;` `    ``Console.WriteLine(CommonDigits(N, M));``}``}` `// This code is contributed by sanjoy_62`

## Javascript

 ``
Output:
`4`

Time Complexity: O(digits(N)+digits(M))
Auxiliary Space: O(10)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up