Open In App

Number of common digits present in two given numbers

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:

Below is the implementation of the above approach:




// 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 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[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 Code
int main()
{
    // Input
    int N = 748294;
    int M = 34298156;
 
    cout << CommonDigits(N, M);
 
    return 0;
}




// 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 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# 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[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
static 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 M
function 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>

Output
4









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

 Using Sets in Python:

Approach:

One of the simplest approaches is to convert both numbers to sets and find the intersection of the two sets. The length of the intersection set will be the number of common digits.




#include <iostream>
#include <string>
#include <unordered_set>
 
using namespace std;
 
int count_common_digits(int n, int m) {
    string n_str = to_string(n);
    string m_str = to_string(m);
 
    unordered_set<char> n_set(n_str.begin(), n_str.end());
    unordered_set<char> m_set(m_str.begin(), m_str.end());
 
    int common_digits = 0;
    for (char digit : n_set) {
        if (m_set.count(digit) > 0) {
            common_digits++;
        }
    }
 
    return common_digits;
}
 
int main() {
    int n = 748294;
    int m = 34298156;
    cout << count_common_digits(n, m) << endl; // Output: 4
 
    return 0;
}




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.HashSet;
import java.util.Set;
 
public class Main {
     
    public static int countCommonDigits(int n, int m) {
        String nStr = Integer.toString(n);
        String mStr = Integer.toString(m);
 
        Set<Character> nSet = new HashSet<>();
        Set<Character> mSet = new HashSet<>();
 
        for (char digit : nStr.toCharArray()) {
            nSet.add(digit);
        }
 
        for (char digit : mStr.toCharArray()) {
            mSet.add(digit);
        }
 
        int commonDigits = 0;
        for (char digit : nSet) {
            if (mSet.contains(digit)) {
                commonDigits++;
            }
        }
 
        return commonDigits;
    }
 
    public static void main(String[] args) {
        int n = 748294;
        int m = 34298156;
        System.out.println(countCommonDigits(n, m)); // Output- 4
    }
}
 
// This code is contributed by guptapratik




def count_common_digits(n, m):
    n_set = set(str(n))
    m_set = set(str(m))
    return len(n_set.intersection(m_set))
 
# Example usage
n = 748294
m = 34298156
print(count_common_digits(n, m)) # Output: 4




using System;
using System.Collections.Generic;
using System.Linq;
 
class Program {
    // Function to count the number of common digits in two
    // integers
    static int CountCommonDigits(int n, int m)
    {
        // Convert integers to strings to work with their
        // digits
        string nStr = n.ToString();
        string mStr = m.ToString();
 
        // Create sets to store unique digits from both
        // integers
        HashSet<char> nSet = new HashSet<char>(nStr);
        HashSet<char> mSet = new HashSet<char>(mStr);
 
        int commonDigits = 0;
 
        // Iterate through the digits of one integer and
        // check if they exist in the other
        foreach(char digit in nSet)
        {
            if (mSet.Contains(digit)) {
                commonDigits++;
            }
        }
 
        return commonDigits;
    }
 
    static void Main()
    {
        int n = 748294;
        int m = 34298156;
 
        int result = CountCommonDigits(n, m);
        Console.WriteLine(result); // Output: 4
    }
}




function countCommonDigits(n, m) {
    // Convert n and m to strings
    const nStr = n.toString();
    const mStr = m.toString();
 
    // Create sets of unique digits for n and m
    const nSet = new Set(nStr);
    const mSet = new Set(mStr);
 
    let commonDigits = 0;
 
    // Iterate through the digits in nSet
    for (const digit of nSet) {
        if (mSet.has(digit)) {
            commonDigits++;
        }
    }
 
    return commonDigits;
}
 
// Test the function with example values
const n = 748294;
const m = 34298156;
console.log(countCommonDigits(n, m)); // Output: 4

Output
4









Time Complexity: O(m + n), where m and n are the numbers of digits in the two numbers respectively.
Space Complexity: O(m + n)


Article Tags :