Number of common digits present in two given numbers
Last Updated :
12 Nov, 2023
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[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
#include <bits/stdc++.h>
using namespace std;
int CommonDigits( int N, int M)
{
int count = 0;
int freq1[10] = { 0 };
int freq2[10] = { 0 };
while (N > 0) {
freq1[N % 10]++;
N = N / 10;
}
while (M > 0) {
freq2[M % 10]++;
M = M / 10;
}
for ( int i = 0; i < 10; i++) {
if (freq1[i] > 0 & freq2[i] > 0) {
count++;
}
}
return count;
}
int main()
{
int N = 748294;
int M = 34298156;
cout << CommonDigits(N, M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int CommonDigits( int N, int M)
{
int count = 0 ;
int freq1[] = new int [ 10 ];
int freq2[] = new int [ 10 ];
while (N > 0 )
{
freq1[N % 10 ]++;
N = N / 10 ;
}
while (M > 0 )
{
freq2[M % 10 ]++;
M = M / 10 ;
}
for ( int i = 0 ; i < 10 ; i++)
{
if (freq1[i] > 0 & freq2[i] > 0 )
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
int N = 748294 ;
int M = 34298156 ;
System.out.print(CommonDigits(N, M));
}
}
|
Python3
def CommonDigits(N, M):
count = 0
freq1 = [ 0 ] * 10
freq2 = [ 0 ] * 10
while (N > 0 ):
freq1[N % 10 ] + = 1
N = N / / 10
while (M > 0 ):
freq2[M % 10 ] + = 1
M = M / / 10
for i in range ( 10 ):
if (freq1[i] > 0 and freq2[i] > 0 ):
count + = 1
return count
if __name__ = = '__main__' :
N = 748294
M = 34298156
print (CommonDigits(N, M))
|
C#
using System;
class GFG{
static int CommonDigits( int N, int M)
{
int count = 0;
int [] freq1 = new int [10];
int [] freq2 = new int [10];
while (N > 0)
{
freq1[N % 10]++;
N = N / 10;
}
while (M > 0)
{
freq2[M % 10]++;
M = M / 10;
}
for ( int i = 0; i < 10; i++)
{
if (freq1[i] > 0 & freq2[i] > 0)
{
count++;
}
}
return count;
}
static void Main()
{
int N = 748294;
int M = 34298156;
Console.WriteLine(CommonDigits(N, M));
}
}
|
Javascript
<script>
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));
</script>
|
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.
- Define the function count_common_digits(n, m) that takes two integer inputs n and m.
- Convert both n and m to sets of digits using the set() function and str() function. Store the sets in variables n_set and m_set, respectively.
- Find the intersection of the two sets using the intersection() method of sets. Store the result in a variable common_digits.
- Return the length of the common_digits set using the len() function as the output of the function
C++
#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;
return 0;
}
|
Java
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));
}
}
|
Python3
def count_common_digits(n, m):
n_set = set ( str (n))
m_set = set ( str (m))
return len (n_set.intersection(m_set))
n = 748294
m = 34298156
print (count_common_digits(n, m))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int CountCommonDigits( int n, int m)
{
string nStr = n.ToString();
string mStr = m.ToString();
HashSet< char > nSet = new HashSet< char >(nStr);
HashSet< char > mSet = new HashSet< char >(mStr);
int commonDigits = 0;
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);
}
}
|
Javascript
function countCommonDigits(n, m) {
const nStr = n.toString();
const mStr = m.toString();
const nSet = new Set(nStr);
const mSet = new Set(mStr);
let commonDigits = 0;
for (const digit of nSet) {
if (mSet.has(digit)) {
commonDigits++;
}
}
return commonDigits;
}
const n = 748294;
const m = 34298156;
console.log(countCommonDigits(n, m));
|
Time Complexity: O(m + n), where m and n are the numbers of digits in the two numbers respectively.
Space Complexity: O(m + n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...