# Number of common digits present in two given numbers

Here given code implementation process.

``````// C Program for
// Number of common digits present in two given numbers
#include <stdio.h>
int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void countCommonDigit(int a, int b)
{
int n = absValue(a);
int count = 0;
// Use to collect digit frequency
int digit[10];
// Set initial frequency of digit 0-9
for (int i = 0; i < 10; ++i)
{
digit[i] = 0;
}
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = n / 10;
}
n = absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = n / 10;
}
printf("\n Given a : %d  b : %d", a, b);
// Count common digit
for (int i = 0; i < 10; ++i)
{
if (digit[i] == 2)
{
count += 1;
}
}
// Display calculated result
printf("\n Result : %d\n", count);
}
int main(int argc, char
const *argv[])
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
countCommonDigit(1241363, 523453);
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
countCommonDigit(421870, 59137);
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
countCommonDigit(534512, -132352);
return 0;
}``````

#### Output

`````` Given a : 1241363  b : 523453
Result : 3

Given a : 421870  b : 59137
Result : 2

Given a : 534512  b : -132352
Result : 4``````
``````// Java program for
// Number of common digits present in two given numbers
public class Counting
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void countCommonDigit(int a, int b)
{
int n = absValue(a);
int count = 0;
// Use to collect digit frequency
int[] digit = new int[10];
// Set initial frequency of digit 0-9
for (int i = 0; i < 10; ++i)
{
digit[i] = 0;
}
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = n / 10;
}
n = absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = n / 10;
}
System.out.print("\n Given a : " + a + " b : " + b);
// Count common digit
for (int i = 0; i < 10; ++i)
{
if (digit[i] == 2)
{
count += 1;
}
}
// Display calculated result
System.out.print("\n Result : " + count + "\n");
}
public static void main(String[] args)
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}
}``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Number of common digits present in two given numbers

class Counting
{
public: int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
void countCommonDigit(int a, int b)
{
int n = this->absValue(a);
int count = 0;
// Use to collect digit frequency
int digit[10];
// Set initial frequency of digit 0-9
for (int i = 0; i < 10; ++i)
{
digit[i] = 0;
}
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = n / 10;
}
n = this->absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = n / 10;
}
cout << "\n Given a : " << a << " b : " << b;
// Count common digit
for (int i = 0; i < 10; ++i)
{
if (digit[i] == 2)
{
count += 1;
}
}
// Display calculated result
cout << "\n Result : " << count << "\n";
}
};
int main()
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
return 0;
}``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````package main
import "fmt"
// Go program for
// Number of common digits present in two given numbers
type Counting struct {}
func getCounting() * Counting {
var me *Counting = &Counting {}
return me
}
func(this Counting) absValue(num int) int {
if num < 0 {
return -num
}
return num
}
func(this Counting) countCommonDigit(a, b int) {
var n int = this.absValue(a)
var count int = 0
// Use to collect digit frequency
var digit = make([] int, 10)
// Set initial frequency of digit 0-9
for i := 0 ; i < 10 ; i++ {
digit[i] = 0
}
// If digits are present in A, then set its frequency to 1.
for (n > 0) {
digit[n % 10] = 1
// Remove last digit
n = n / 10
}
n = this.absValue(b)
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
for (n > 0) {
if digit[n % 10] == 1 {
digit[n % 10] = 2
}
// Remove last digit
n = n / 10
}
fmt.Print("\n Given a : ", a, " b : ", b)
// Count common digit
for i := 0 ; i < 10 ; i++ {
if digit[i] == 2 {
count += 1
}
}
// Display calculated result
fmt.Print("\n Result : ", count, "\n")
}
func main() {
var task * Counting = getCounting()
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````// Include namespace system
using System;
// Csharp program for
// Number of common digits present in two given numbers
public class Counting
{
public int absValue(int num)
{
if (num < 0)
{
return -num;
}
return num;
}
public void countCommonDigit(int a, int b)
{
int n = this.absValue(a);
int count = 0;
// Use to collect digit frequency
int[] digit = new int[10];
// Set initial frequency of digit 0-9
for (int i = 0; i < 10; ++i)
{
digit[i] = 0;
}
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = n / 10;
}
n = this.absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = n / 10;
}
Console.Write("\n Given a : " + a + " b : " + b);
// Count common digit
for (int i = 0; i < 10; ++i)
{
if (digit[i] == 2)
{
count += 1;
}
}
// Display calculated result
Console.Write("\n Result : " + count + "\n");
}
public static void Main(String[] args)
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}
}``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````<?php
// Php program for
// Number of common digits present in two given numbers
class Counting
{
public	function absValue(\$num)
{
if (\$num < 0)
{
return -\$num;
}
return \$num;
}
public	function countCommonDigit(\$a, \$b)
{
\$n = \$this->absValue(\$a);
\$count = 0;
// Use to collect digit frequency
\$digit = array_fill(0, 10, 0);
// If digits are present in A, then set its frequency to 1.
while (\$n > 0)
{
\$digit[\$n % 10] = 1;
// Remove last digit
\$n = (int)(\$n / 10);
}
\$n = \$this->absValue(\$b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (\$n > 0)
{
if (\$digit[\$n % 10] == 1)
{
\$digit[\$n % 10] = 2;
}
// Remove last digit
\$n = (int)(\$n / 10);
}
echo("\n Given a : ".\$a.
" b : ".\$b);
// Count common digit
for (\$i = 0; \$i < 10; ++\$i)
{
if (\$digit[\$i] == 2)
{
\$count += 1;
}
}
// Display calculated result
echo("\n Result : ".\$count."\n");
}
}

function main()
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}
main();``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````// Node JS program for
// Number of common digits present in two given numbers
class Counting
{
absValue(num)
{
if (num < 0)
{
return -num;
}
return num;
}
countCommonDigit(a, b)
{
var n = this.absValue(a);
var count = 0;
// Use to collect digit frequency
var digit = Array(10).fill(0);
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = parseInt(n / 10);
}
n = this.absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = parseInt(n / 10);
}
process.stdout.write("\n Given a : " + a + " b : " + b);
// Count common digit
for (var i = 0; i < 10; ++i)
{
if (digit[i] == 2)
{
count += 1;
}
}
// Display calculated result
process.stdout.write("\n Result : " + count + "\n");
}
}

function main()
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}
main();``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````#  Python 3 program for
#  Number of common digits present in two given numbers
class Counting :
def absValue(self, num) :
if (num < 0) :
return -num

return num

def countCommonDigit(self, a, b) :
n = self.absValue(a)
count = 0
#  Use to collect digit frequency
digit = [0] * (10)
#  If digits are present in A, then set its frequency to 1.
while (n > 0) :
digit[n % 10] = 1
#  Remove last digit
n = int(n / 10)

n = self.absValue(b)
#  Set the normal digit frequency to 2 when
#  numbers are present in A and B.
while (n > 0) :
if (digit[n % 10] == 1) :
digit[n % 10] = 2

#  Remove last digit
n = int(n / 10)

print("\n Given a : ", a ," b : ", b, end = "")
i = 0
#  Count common digit
while (i < 10) :
if (digit[i] == 2) :
count += 1

i += 1

#  Display calculated result
print("\n Result : ", count )

def main() :
#  Test A
#  a = 1241363  b = 523453
#  common = [2 3 4]
#  Output = 3
#  Test B
#  a = 421870  b = 59137
#  common = [1 7]
#  Output = 2
#  Test C
#  a = 534512  b = -132352
#  common = [1 2 3 5]
#  Output = 4

if __name__ == "__main__": main()``````

#### Output

`````` Given a :  1241363  b :  523453
Result :  3

Given a :  421870  b :  59137
Result :  2

Given a :  534512  b :  -132352
Result :  4``````
``````#  Ruby program for
#  Number of common digits present in two given numbers
class Counting
def absValue(num)
if (num < 0)
return -num
end

return num
end

def countCommonDigit(a, b)
n = self.absValue(a)
count = 0
#  Use to collect digit frequency
digit = Array.new(10) {0}
#  If digits are present in A, then set its frequency to 1.
while (n > 0)
digit[n % 10] = 1
#  Remove last digit
n = n / 10
end

n = self.absValue(b)
#  Set the normal digit frequency to 2 when
#  numbers are present in A and B.
while (n > 0)
if (digit[n % 10] == 1)
digit[n % 10] = 2
end

#  Remove last digit
n = n / 10
end

print("\n Given a : ", a ," b : ", b)
i = 0
#  Count common digit
while (i < 10)
if (digit[i] == 2)
count += 1
end

i += 1
end

#  Display calculated result
print("\n Result : ", count ,"\n")
end

end

def main()
#  Test A
#  a = 1241363  b = 523453
#  common = [2 3 4]
#  Output = 3
#  Test B
#  a = 421870  b = 59137
#  common = [1 7]
#  Output = 2
#  Test C
#  a = 534512  b = -132352
#  common = [1 2 3 5]
#  Output = 4
end

main()``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4
``````
``````// Scala program for
// Number of common digits present in two given numbers
class Counting()
{
def absValue(num: Int): Int = {
if (num < 0)
{
return -num;
}
return num;
}
def countCommonDigit(a: Int, b: Int): Unit = {
var n: Int = absValue(a);
var count: Int = 0;
// Use to collect digit frequency
var digit: Array[Int] = Array.fill[Int](10)(0);
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit(n % 10) = 1;
// Remove last digit
n = n / 10;
}
n = absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit(n % 10) == 1)
{
digit(n % 10) = 2;
}
// Remove last digit
n = n / 10;
}
print("\n Given a : " + a + " b : " + b);
var i: Int = 0;
// Count common digit
while (i < 10)
{
if (digit(i) == 2)
{
count += 1;
}
i += 1;
}
// Display calculated result
print("\n Result : " + count + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}
}``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````
``````// Swift 4 program for
// Number of common digits present in two given numbers
class Counting
{
func absValue(_ num: Int) -> Int
{
if (num < 0)
{
return -num;
}
return num;
}
func countCommonDigit(_ a: Int, _ b: Int)
{
var n: Int = self.absValue(a);
var count: Int = 0;
// Use to collect digit frequency
var digit: [Int] = Array(repeating: 0, count: 10);
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = n / 10;
}
n = self.absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = n / 10;
}
print("\n Given a : ", a ," b : ", b, terminator: "");
var i: Int = 0;
// Count common digit
while (i < 10)
{
if (digit[i] == 2)
{
count += 1;
}
i += 1;
}
// Display calculated result
print("\n Result : ", count );
}
}
func main()
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}
main();``````

#### Output

`````` Given a :  1241363  b :  523453
Result :  3

Given a :  421870  b :  59137
Result :  2

Given a :  534512  b :  -132352
Result :  4``````
``````// Kotlin program for
// Number of common digits present in two given numbers
class Counting
{
fun absValue(num: Int): Int
{
if (num < 0)
{
return -num;
}
return num;
}
fun countCommonDigit(a: Int, b: Int): Unit
{
var n: Int = this.absValue(a);
var count: Int = 0;
// Use to collect digit frequency
var digit: Array < Int > = Array(10)
{
0
};
// If digits are present in A, then set its frequency to 1.
while (n > 0)
{
digit[n % 10] = 1;
// Remove last digit
n = n / 10;
}
n = this.absValue(b);
// Set the normal digit frequency to 2 when
// numbers are present in A and B.
while (n > 0)
{
if (digit[n % 10] == 1)
{
digit[n % 10] = 2;
}
// Remove last digit
n = n / 10;
}
print("\n Given a : " + a + " b : " + b);
var i: Int = 0;
// Count common digit
while (i < 10)
{
if (digit[i] == 2)
{
count += 1;
}
i += 1;
}
// Display calculated result
print("\n Result : " + count + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// Test A
// a = 1241363  b = 523453
// common = [2 3 4]
// Output = 3
// Test B
// a = 421870  b = 59137
// common = [1 7]
// Output = 2
// Test C
// a = 534512  b = -132352
// common = [1 2 3 5]
// Output = 4
}``````

#### Output

`````` Given a : 1241363 b : 523453
Result : 3

Given a : 421870 b : 59137
Result : 2

Given a : 534512 b : -132352
Result : 4``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.