Print all distinct uncommon digits present in two given numbers

Here given code implementation process.

// C Program for
// Print all distinct uncommon digits present in two given numbers
#include <stdio.h>

int absValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
void unCommonDigit(int a, int b)
{
	int n = absValue(a);
	int count = 0;
	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);
	while (n > 0)
	{
		if (digit[n % 10] == 0)
		{
			// new digit in B number
			digit[n % 10] = 3;
		}
		else if (digit[n % 10] == 1)
		{
			// Common digit
			digit[n % 10] = 2;
		}
		// Remove last digit
		n = n / 10;
	}
	printf("\n Given a : %d  b : %d\n", a, b);
	for (int i = 0; i < 10; ++i)
	{
		if (digit[i] == 1 || digit[i] == 3)
		{
			count++;
			printf(" %d", i);
		}
	}
	if (count == 0)
	{
		printf("\n None \n");
	}
}
int main(int argc, char
	const *argv[])
{
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	unCommonDigit(1241363, 523753);
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	unCommonDigit(421870, 59137);
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	unCommonDigit(534512, -137352);
	return 0;
}

Output

 Given a : 1241363  b : 523753
 1 4 5 6 7
 Given a : 421870  b : 59137
 0 2 3 4 5 8 9
 Given a : 534512  b : -137352
 4 7
// Java program for
// Print all distinct uncommon digits present in two given numbers
public class UncommonDigits
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void unCommonDigit(int a, int b)
	{
		int n = absValue(a);
		boolean status = false;
		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);
		while (n > 0)
		{
			if (digit[n % 10] == 0)
			{
				// new digit in B number
				digit[n % 10] = 3;
			}
			else if (digit[n % 10] == 1)
			{
				// Common digit
				digit[n % 10] = 2;
			}
			// Remove last digit
			n = n / 10;
		}
		System.out.print("\n Given a : " + a + " b : " + b + "\n");
		for (int i = 0; i < 10; ++i)
		{
			if (digit[i] == 1 || digit[i] == 3)
			{
				status = true;
				System.out.print(" " + i);
			}
		}
		if (status == false)
		{
			System.out.print("\n None \n");
		}
	}
	public static void main(String[] args)
	{
		UncommonDigits task = new UncommonDigits();
		// Test A
		// a = 1241363  b = 523753
		// uncommon digit = [1 4 5 6 7]
		task.unCommonDigit(1241363, 523753);
		// Test B
		// a = 421870  b = 59137
		// uncommon digit = [0 2 3 4 5 8 9] 
		task.unCommonDigit(421870, 59137);
		// Test C
		// a = 534512  b = -137352
		// uncommon digit = [4 7]
		task.unCommonDigit(534512, -137352);
	}
}

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Print all distinct uncommon digits present in two given numbers
class UncommonDigits
{
	public: int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void unCommonDigit(int a, int b)
	{
		int n = this->absValue(a);
		bool status = false;
		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);
		while (n > 0)
		{
			if (digit[n % 10] == 0)
			{
				// new digit in B number
				digit[n % 10] = 3;
			}
			else if (digit[n % 10] == 1)
			{
				// Common digit
				digit[n % 10] = 2;
			}
			// Remove last digit
			n = n / 10;
		}
		cout << "\n Given a : " << a << " b : " << b << "\n";
		for (int i = 0; i < 10; ++i)
		{
			if (digit[i] == 1 || digit[i] == 3)
			{
				status = true;
				cout << " " << i;
			}
		}
		if (status == false)
		{
			cout << "\n None \n";
		}
	}
};
int main()
{
	UncommonDigits *task = new UncommonDigits();
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	task->unCommonDigit(1241363, 523753);
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	task->unCommonDigit(421870, 59137);
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	task->unCommonDigit(534512, -137352);
	return 0;
}

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
package main
import "fmt"
// Go program for
// Print all distinct uncommon digits present in two given numbers
type UncommonDigits struct {}
func getUncommonDigits() * UncommonDigits {
	var me *UncommonDigits = &UncommonDigits {}
	return me
}
func(this UncommonDigits) absValue(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
func(this UncommonDigits) unCommonDigit(a, b int) {
	var n int = this.absValue(a)
	var status bool = false
	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)
	for (n > 0) {
		if digit[n % 10] == 0 {
			// new digit in B number
			digit[n % 10] = 3
		} else if digit[n % 10] == 1 {
			// Common digit
			digit[n % 10] = 2
		}
		// Remove last digit
		n = n / 10
	}
	fmt.Print("\n Given a : ", a, " b : ", b, "\n")
	for i := 0 ; i < 10 ; i++ {
		if digit[i] == 1 || digit[i] == 3 {
			status = true
			fmt.Print(" ", i)
		}
	}
	if status == false {
		fmt.Print("\n None \n")
	}
}
func main() {
	var task * UncommonDigits = getUncommonDigits()
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	task.unCommonDigit(1241363, 523753)
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	task.unCommonDigit(421870, 59137)
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	task.unCommonDigit(534512, -137352)
}

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
// Include namespace system
using System;
// Csharp program for
// Print all distinct uncommon digits present in two given numbers
public class UncommonDigits
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void unCommonDigit(int a, int b)
	{
		int n = this.absValue(a);
		Boolean status = false;
		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);
		while (n > 0)
		{
			if (digit[n % 10] == 0)
			{
				// new digit in B number
				digit[n % 10] = 3;
			}
			else if (digit[n % 10] == 1)
			{
				// Common digit
				digit[n % 10] = 2;
			}
			// Remove last digit
			n = n / 10;
		}
		Console.Write("\n Given a : " + a + " b : " + b + "\n");
		for (int i = 0; i < 10; ++i)
		{
			if (digit[i] == 1 || digit[i] == 3)
			{
				status = true;
				Console.Write(" " + i);
			}
		}
		if (status == false)
		{
			Console.Write("\n None \n");
		}
	}
	public static void Main(String[] args)
	{
		UncommonDigits task = new UncommonDigits();
		// Test A
		// a = 1241363  b = 523753
		// uncommon digit = [1 4 5 6 7]
		task.unCommonDigit(1241363, 523753);
		// Test B
		// a = 421870  b = 59137
		// uncommon digit = [0 2 3 4 5 8 9] 
		task.unCommonDigit(421870, 59137);
		// Test C
		// a = 534512  b = -137352
		// uncommon digit = [4 7]
		task.unCommonDigit(534512, -137352);
	}
}

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
<?php
// Php program for
// Print all distinct uncommon digits present in two given numbers
class UncommonDigits
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function unCommonDigit($a, $b)
	{
		$n = $this->absValue($a);
		$status = false;
		$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);
		while ($n > 0)
		{
			if ($digit[$n % 10] == 0)
			{
				// new digit in B number
				$digit[$n % 10] = 3;
			}
			else if ($digit[$n % 10] == 1)
			{
				// Common digit
				$digit[$n % 10] = 2;
			}
			// Remove last digit
			$n = (int)($n / 10);
		}
		echo("\n Given a : ".$a.
			" b : ".$b.
			"\n");
		for ($i = 0; $i < 10; ++$i)
		{
			if ($digit[$i] == 1 || $digit[$i] == 3)
			{
				$status = true;
				echo(" ".$i);
			}
		}
		if ($status == false)
		{
			echo("\n None \n");
		}
	}
}

function main()
{
	$task = new UncommonDigits();
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	$task->unCommonDigit(1241363, 523753);
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	$task->unCommonDigit(421870, 59137);
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	$task->unCommonDigit(534512, -137352);
}
main();

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
// Node JS program for
// Print all distinct uncommon digits present in two given numbers
class UncommonDigits
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	unCommonDigit(a, b)
	{
		var n = this.absValue(a);
		var status = false;
		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);
		while (n > 0)
		{
			if (digit[n % 10] == 0)
			{
				// new digit in B number
				digit[n % 10] = 3;
			}
			else if (digit[n % 10] == 1)
			{
				// Common digit
				digit[n % 10] = 2;
			}
			// Remove last digit
			n = parseInt(n / 10);
		}
		process.stdout.write("\n Given a : " + a + " b : " + b + "\n");
		for (var i = 0; i < 10; ++i)
		{
			if (digit[i] == 1 || digit[i] == 3)
			{
				status = true;
				process.stdout.write(" " + i);
			}
		}
		if (status == false)
		{
			process.stdout.write("\n None \n");
		}
	}
}

function main()
{
	var task = new UncommonDigits();
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	task.unCommonDigit(1241363, 523753);
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	task.unCommonDigit(421870, 59137);
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	task.unCommonDigit(534512, -137352);
}
main();

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
#  Python 3 program for
#  Print all distinct uncommon digits present in two given numbers
class UncommonDigits :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def unCommonDigit(self, a, b) :
		n = self.absValue(a)
		status = False
		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)
		while (n > 0) :
			if (digit[n % 10] == 0) :
				#  new digit in B number
				digit[n % 10] = 3
			elif (digit[n % 10] == 1) :
				#  Common digit
				digit[n % 10] = 2
			
			#  Remove last digit
			n = int(n / 10)
		
		print("\n Given a : ", a ," b : ", b )
		i = 0
		while (i < 10) :
			if (digit[i] == 1 or digit[i] == 3) :
				status = True
				print(" ", i, end = "")
			
			i += 1
		
		if (status == False) :
			print("\n None ")
		
	

def main() :
	task = UncommonDigits()
	#  Test A
	#  a = 1241363  b = 523753
	#  uncommon digit = [1 4 5 6 7]
	task.unCommonDigit(1241363, 523753)
	#  Test B
	#  a = 421870  b = 59137
	#  uncommon digit = [0 2 3 4 5 8 9] 
	task.unCommonDigit(421870, 59137)
	#  Test C
	#  a = 534512  b = -137352
	#  uncommon digit = [4 7]
	task.unCommonDigit(534512, -137352)

if __name__ == "__main__": main()

Output

 Given a :  1241363  b :  523753
  1  4  5  6  7
 Given a :  421870  b :  59137
  0  2  3  4  5  8  9
 Given a :  534512  b :  -137352
  4  7
#  Ruby program for
#  Print all distinct uncommon digits present in two given numbers
class UncommonDigits 
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def unCommonDigit(a, b) 
		n = self.absValue(a)
		status = false
		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)
		while (n > 0) 
			if (digit[n % 10] == 0) 
				#  new digit in B number
				digit[n % 10] = 3
			elsif (digit[n % 10] == 1) 
				#  Common digit
				digit[n % 10] = 2
			end

			#  Remove last digit
			n = n / 10
		end

		print("\n Given a : ", a ," b : ", b ,"\n")
		i = 0
		while (i < 10) 
			if (digit[i] == 1 || digit[i] == 3) 
				status = true
				print(" ", i)
			end

			i += 1
		end

		if (status == false) 
			print("\n None \n")
		end

	end

end

def main() 
	task = UncommonDigits.new()
	#  Test A
	#  a = 1241363  b = 523753
	#  uncommon digit = [1 4 5 6 7]
	task.unCommonDigit(1241363, 523753)
	#  Test B
	#  a = 421870  b = 59137
	#  uncommon digit = [0 2 3 4 5 8 9] 
	task.unCommonDigit(421870, 59137)
	#  Test C
	#  a = 534512  b = -137352
	#  uncommon digit = [4 7]
	task.unCommonDigit(534512, -137352)
end

main()

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
// Scala program for
// Print all distinct uncommon digits present in two given numbers
class UncommonDigits()
{
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def unCommonDigit(a: Int, b: Int): Unit = {
		var n: Int = absValue(a);
		var status: Boolean = false;
		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);
		while (n > 0)
		{
			if (digit(n % 10) == 0)
			{
				// new digit in B number
				digit(n % 10) = 3;
			}
			else if (digit(n % 10) == 1)
			{
				// Common digit
				digit(n % 10) = 2;
			}
			// Remove last digit
			n = n / 10;
		}
		print("\n Given a : " + a + " b : " + b + "\n");
		var i: Int = 0;
		while (i < 10)
		{
			if (digit(i) == 1 || digit(i) == 3)
			{
				status = true;
				print(" " + i);
			}
			i += 1;
		}
		if (status == false)
		{
			print("\n None \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: UncommonDigits = new UncommonDigits();
		// Test A
		// a = 1241363  b = 523753
		// uncommon digit = [1 4 5 6 7]
		task.unCommonDigit(1241363, 523753);
		// Test B
		// a = 421870  b = 59137
		// uncommon digit = [0 2 3 4 5 8 9] 
		task.unCommonDigit(421870, 59137);
		// Test C
		// a = 534512  b = -137352
		// uncommon digit = [4 7]
		task.unCommonDigit(534512, -137352);
	}
}

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7
// Swift 4 program for
// Print all distinct uncommon digits present in two given numbers
class UncommonDigits
{
	func absValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func unCommonDigit(_ a: Int, _ b: Int)
	{
		var n: Int = self.absValue(a);
		var status: Bool = false;
		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);
		while (n > 0)
		{
			if (digit[n % 10] == 0)
			{
				// new digit in B number
				digit[n % 10] = 3;
			}
			else if (digit[n % 10] == 1)
			{
				// Common digit
				digit[n % 10] = 2;
			}
			// Remove last digit
			n = n / 10;
		}
		print("\n Given a : ", a ," b : ", b );
		var i: Int = 0;
		while (i < 10)
		{
			if (digit[i] == 1 || digit[i] == 3)
			{
				status = true;
				print(" ", i, terminator: "");
			}
			i += 1;
		}
		if (status == false)
		{
			print("\n None ");
		}
	}
}
func main()
{
	let task: UncommonDigits = UncommonDigits();
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	task.unCommonDigit(1241363, 523753);
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	task.unCommonDigit(421870, 59137);
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	task.unCommonDigit(534512, -137352);
}
main();

Output

 Given a :  1241363  b :  523753
  1  4  5  6  7
 Given a :  421870  b :  59137
  0  2  3  4  5  8  9
 Given a :  534512  b :  -137352
  4  7
// Kotlin program for
// Print all distinct uncommon digits present in two given numbers
class UncommonDigits
{
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun unCommonDigit(a: Int, b: Int): Unit
	{
		var n: Int = this.absValue(a);
		var status: Boolean = false;
		val 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);
		while (n > 0)
		{
			if (digit[n % 10] == 0)
			{
				// new digit in B number
				digit[n % 10] = 3;
			}
			else if (digit[n % 10] == 1)
			{
				// Common digit
				digit[n % 10] = 2;
			}
			// Remove last digit
			n = n / 10;
		}
		print("\n Given a : " + a + " b : " + b + "\n");
		var i: Int = 0;
		while (i < 10)
		{
			if (digit[i] == 1 || digit[i] == 3)
			{
				status = true;
				print(" " + i);
			}
			i += 1;
		}
		if (status == false)
		{
			print("\n None \n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: UncommonDigits = UncommonDigits();
	// Test A
	// a = 1241363  b = 523753
	// uncommon digit = [1 4 5 6 7]
	task.unCommonDigit(1241363, 523753);
	// Test B
	// a = 421870  b = 59137
	// uncommon digit = [0 2 3 4 5 8 9] 
	task.unCommonDigit(421870, 59137);
	// Test C
	// a = 534512  b = -137352
	// uncommon digit = [4 7]
	task.unCommonDigit(534512, -137352);
}

Output

 Given a : 1241363 b : 523753
 1 4 5 6 7
 Given a : 421870 b : 59137
 0 2 3 4 5 8 9
 Given a : 534512 b : -137352
 4 7


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.

New Comment







© 2021, kalkicode.com, All rights reserved