Check if two numbers have same digits

Here given code implementation process.

// C Program for
// Check if two numbers have same digits 
#include <stdio.h>

int absValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
void isSameDigit(int a, int b)
{
	int n = absValue(a);
	int count[10];
	for (int i = 0; i < 10; ++i)
	{
		count[i] = 0;
	}
	// Count digit frequency of number A
	while (n > 0)
	{
		count[n % 10]++;
		n = n / 10;
	}
	n = absValue(b);
	// Reduce frequency digit of number B
	while (n > 0)
	{
		count[n % 10]--;
		n = n / 10;
	}
	printf("\n Given number a : %d  b : %d", a, b);
	for (int i = 0; i < 10; ++i)
	{
		if (count[i] != 0)
		{
			// When No
			printf("\n Both not contain same digits");
			return;
		}
	}
	printf("\n Both contain same digits");
}
int main(int argc, char const *argv[])
{
	// Test
	isSameDigit(2311, 1321);
	isSameDigit(1311, 111);
	isSameDigit(1321, -2113);
	return 0;
}

Output

 Given number a : 2311  b : 1321
 Both contain same digits
 Given number a : 1311  b : 111
 Both not contain same digits
 Given number a : 1321  b : -2113
 Both contain same digits
// Java program for
// Check if two numbers have same digits 
public class EqualDigits
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void isSameDigit(int a, int b)
	{
		int n = absValue(a);
		int[] count = new int[10];
		for (int i = 0; i < 10; ++i)
		{
			count[i] = 0;
		}
		// Count digit frequency of number A
		while (n > 0)
		{
			count[n % 10]++;
			n = n / 10;
		}
		n = absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count[n % 10]--;
			n = n / 10;
		}
		System.out.print("\n Given number a : " + a + " b : " + b);
		for (int i = 0; i < 10; ++i)
		{
			if (count[i] != 0)
			{
				// When No
				System.out.print("\n Both not contain same digits");
				return;
			}
		}
		System.out.print("\n Both contain same digits");
	}
	public static void main(String[] args)
	{
		EqualDigits task = new EqualDigits();
		// Test
		task.isSameDigit(2311, 1321);
		task.isSameDigit(1311, 111);
		task.isSameDigit(1321, -2113);
	}
}

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Check if two numbers have same digits 
class EqualDigits
{
	public: int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void isSameDigit(int a, int b)
	{
		int n = this->absValue(a);
		int count[10];
		for (int i = 0; i < 10; ++i)
		{
			count[i] = 0;
		}
		// Count digit frequency of number A
		while (n > 0)
		{
			count[n % 10]++;
			n = n / 10;
		}
		n = this->absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count[n % 10]--;
			n = n / 10;
		}
		cout << "\n Given number a : " << a << " b : " << b;
		for (int i = 0; i < 10; ++i)
		{
			if (count[i] != 0)
			{
				// When No
				cout << "\n Both not contain same digits";
				return;
			}
		}
		cout << "\n Both contain same digits";
	}
};
int main()
{
	EqualDigits *task = new EqualDigits();
	// Test
	task->isSameDigit(2311, 1321);
	task->isSameDigit(1311, 111);
	task->isSameDigit(1321, -2113);
	return 0;
}

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
package main
import "fmt"
// Go program for
// Check if two numbers have same digits 
type EqualDigits struct {}
func getEqualDigits() * EqualDigits {
	var me *EqualDigits = &EqualDigits {}
	return me
}
func(this EqualDigits) absValue(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
func(this EqualDigits) isSameDigit(a, b int) {
	var n int = this.absValue(a)
	var count = make([] int, 10)
	for i := 0 ; i < 10 ; i++ {
		count[i] = 0
	}
	// Count digit frequency of number A
	for (n > 0) {
		count[n % 10]++
		n = n / 10
	}
	n = this.absValue(b)
	// Reduce frequency digit of number B
	for (n > 0) {
		count[n % 10]--
		n = n / 10
	}
	fmt.Print("\n Given number a : ", a, " b : ", b)
	for i := 0 ; i < 10 ; i++ {
		if count[i] != 0 {
			// When No
			fmt.Print("\n Both not contain same digits")
			return
		}
	}
	fmt.Print("\n Both contain same digits")
}
func main() {
	var task * EqualDigits = getEqualDigits()
	// Test
	task.isSameDigit(2311, 1321)
	task.isSameDigit(1311, 111)
	task.isSameDigit(1321, -2113)
}

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
// Include namespace system
using System;
// Csharp program for
// Check if two numbers have same digits 
public class EqualDigits
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void isSameDigit(int a, int b)
	{
		int n = this.absValue(a);
		int[] count = new int[10];
		for (int i = 0; i < 10; ++i)
		{
			count[i] = 0;
		}
		// Count digit frequency of number A
		while (n > 0)
		{
			count[n % 10]++;
			n = n / 10;
		}
		n = this.absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count[n % 10]--;
			n = n / 10;
		}
		Console.Write("\n Given number a : " + a + " b : " + b);
		for (int i = 0; i < 10; ++i)
		{
			if (count[i] != 0)
			{
				// When No
				Console.Write("\n Both not contain same digits");
				return;
			}
		}
		Console.Write("\n Both contain same digits");
	}
	public static void Main(String[] args)
	{
		EqualDigits task = new EqualDigits();
		// Test
		task.isSameDigit(2311, 1321);
		task.isSameDigit(1311, 111);
		task.isSameDigit(1321, -2113);
	}
}

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
<?php
// Php program for
// Check if two numbers have same digits 
class EqualDigits
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function isSameDigit($a, $b)
	{
		$n = $this->absValue($a);
		$count = array_fill(0, 10, 0);
		// Count digit frequency of number A
		while ($n > 0)
		{
			$count[$n % 10]++;
			$n = (int)($n / 10);
		}
		$n = $this->absValue($b);
		// Reduce frequency digit of number B
		while ($n > 0)
		{
			$count[$n % 10]--;
			$n = (int)($n / 10);
		}
		echo("\n Given number a : ".$a.
			" b : ".$b);
		for ($i = 0; $i < 10; ++$i)
		{
			if ($count[$i] != 0)
			{
				// When No
				echo("\n Both not contain same digits");
				return;
			}
		}
		echo("\n Both contain same digits");
	}
}

function main()
{
	$task = new EqualDigits();
	// Test
	$task->isSameDigit(2311, 1321);
	$task->isSameDigit(1311, 111);
	$task->isSameDigit(1321, -2113);
}
main();

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
// Node JS program for
// Check if two numbers have same digits 
class EqualDigits
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	isSameDigit(a, b)
	{
		var n = this.absValue(a);
		var count = Array(10).fill(0);
		// Count digit frequency of number A
		while (n > 0)
		{
			count[n % 10]++;
			n = parseInt(n / 10);
		}
		n = this.absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count[n % 10]--;
			n = parseInt(n / 10);
		}
		process.stdout.write("\n Given number a : " + a + " b : " + b);
		for (var i = 0; i < 10; ++i)
		{
			if (count[i] != 0)
			{
				// When No
				process.stdout.write("\n Both not contain same digits");
				return;
			}
		}
		process.stdout.write("\n Both contain same digits");
	}
}

function main()
{
	var task = new EqualDigits();
	// Test
	task.isSameDigit(2311, 1321);
	task.isSameDigit(1311, 111);
	task.isSameDigit(1321, -2113);
}
main();

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
#  Python 3 program for
#  Check if two numbers have same digits 
class EqualDigits :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def isSameDigit(self, a, b) :
		n = self.absValue(a)
		count = [0] * (10)
		#  Count digit frequency of number A
		while (n > 0) :
			count[n % 10] += 1
			n = int(n / 10)
		
		n = self.absValue(b)
		#  Reduce frequency digit of number B
		while (n > 0) :
			count[n % 10] -= 1
			n = int(n / 10)
		
		print("\n Given number a : ", a ," b : ", b, end = "")
		i = 0
		while (i < 10) :
			if (count[i] != 0) :
				#  When No
				print("\n Both not contain same digits", end = "")
				return
			
			i += 1
		
		print("\n Both contain same digits", end = "")
	

def main() :
	task = EqualDigits()
	#  Test
	task.isSameDigit(2311, 1321)
	task.isSameDigit(1311, 111)
	task.isSameDigit(1321, -2113)

if __name__ == "__main__": main()

Output

 Given number a :  2311  b :  1321
 Both contain same digits
 Given number a :  1311  b :  111
 Both not contain same digits
 Given number a :  1321  b :  -2113
 Both contain same digits
#  Ruby program for
#  Check if two numbers have same digits 
class EqualDigits 
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def isSameDigit(a, b) 
		n = self.absValue(a)
		count = Array.new(10) {0}
		#  Count digit frequency of number A
		while (n > 0) 
			count[n % 10] += 1
			n = n / 10
		end

		n = self.absValue(b)
		#  Reduce frequency digit of number B
		while (n > 0) 
			count[n % 10] -= 1
			n = n / 10
		end

		print("\n Given number a : ", a ," b : ", b)
		i = 0
		while (i < 10) 
			if (count[i] != 0) 
				#  When No
				print("\n Both not contain same digits")
				return
			end

			i += 1
		end

		print("\n Both contain same digits")
	end

end

def main() 
	task = EqualDigits.new()
	#  Test
	task.isSameDigit(2311, 1321)
	task.isSameDigit(1311, 111)
	task.isSameDigit(1321, -2113)
end

main()

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
// Scala program for
// Check if two numbers have same digits 
class EqualDigits()
{
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def isSameDigit(a: Int, b: Int): Unit = {
		var n: Int = absValue(a);
		var count: Array[Int] = Array.fill[Int](10)(0);
		// Count digit frequency of number A
		while (n > 0)
		{
			count(n % 10) += 1;
			n = n / 10;
		}
		n = absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count(n % 10) -= 1;
			n = n / 10;
		}
		print("\n Given number a : " + a + " b : " + b);
		var i: Int = 0;
		while (i < 10)
		{
			if (count(i) != 0)
			{
				// When No
				print("\n Both not contain same digits");
				return;
			}
			i += 1;
		}
		print("\n Both contain same digits");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: EqualDigits = new EqualDigits();
		// Test
		task.isSameDigit(2311, 1321);
		task.isSameDigit(1311, 111);
		task.isSameDigit(1321, -2113);
	}
}

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits
// Swift 4 program for
// Check if two numbers have same digits 
class EqualDigits
{
	func absValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func isSameDigit(_ a: Int, _ b: Int)
	{
		var n: Int = self.absValue(a);
		var count: [Int] = Array(repeating: 0, count: 10);
		// Count digit frequency of number A
		while (n > 0)
		{
			count[n % 10] += 1;
			n = n / 10;
		}
		n = self.absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count[n % 10] -= 1;
			n = n / 10;
		}
		print("\n Given number a : ", a ," b : ", b, terminator: "");
		var i: Int = 0;
		while (i < 10)
		{
			if (count[i]  != 0)
			{
				// When No
				print("\n Both not contain same digits", terminator: "");
				return;
			}
			i += 1;
		}
		print("\n Both contain same digits", terminator: "");
	}
}
func main()
{
	let task: EqualDigits = EqualDigits();
	// Test
	task.isSameDigit(2311, 1321);
	task.isSameDigit(1311, 111);
	task.isSameDigit(1321, -2113);
}
main();

Output

 Given number a :  2311  b :  1321
 Both contain same digits
 Given number a :  1311  b :  111
 Both not contain same digits
 Given number a :  1321  b :  -2113
 Both contain same digits
// Kotlin program for
// Check if two numbers have same digits 
class EqualDigits
{
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun isSameDigit(a: Int, b: Int): Unit
	{
		var n: Int = this.absValue(a);
		val count: Array < Int > = Array(10)
		{
			0
		};
		// Count digit frequency of number A
		while (n > 0)
		{
			count[n % 10] += 1;
			n = n / 10;
		}
		n = this.absValue(b);
		// Reduce frequency digit of number B
		while (n > 0)
		{
			count[n % 10] -= 1;
			n = n / 10;
		}
		print("\n Given number a : " + a + " b : " + b);
		var i: Int = 0;
		while (i < 10)
		{
			if (count[i] != 0)
			{
				// When No
				print("\n Both not contain same digits");
				return;
			}
			i += 1;
		}
		print("\n Both contain same digits");
	}
}
fun main(args: Array < String > ): Unit
{
	val task: EqualDigits = EqualDigits();
	// Test
	task.isSameDigit(2311, 1321);
	task.isSameDigit(1311, 111);
	task.isSameDigit(1321, -2113);
}

Output

 Given number a : 2311 b : 1321
 Both contain same digits
 Given number a : 1311 b : 111
 Both not contain same digits
 Given number a : 1321 b : -2113
 Both contain same digits


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