Print uncommon elements from two arrays

Here given code implementation process.

import java.util.HashMap;
/*
    Java Program
    Print uncommon elements from two arrays
*/
public class Search
{
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	public void findUncommon(int[] arr1, int[] arr2, int n, int m)
	{
		HashMap < Integer, Integer > record = 
          new HashMap < Integer, Integer > ();
		boolean result = false;
		for (int i = 0; i < n; ++i)
		{
			if (!record.containsKey(arr1[i]))
			{
				// Find distinct elements in first array
				record.put(arr1[i], 1);
			}
		}
		for (int i = 0; i < m; ++i)
		{
			if (!record.containsKey(arr2[i]))
			{
				// Find new element in second array
				record.put(arr2[i], 2);
			}
			else if (record.get(arr2[i]) == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record.put(arr2[i], 3);
			}
		}
		System.out.print("\n Array A  :");
		// Display given arrays
		printArray(arr1, n);
		System.out.print(" Array B  :");
		printArray(arr2, m);
		System.out.print(" Uncommon :");
		for (int v: record.keySet())
		{
			if (record.get(v) != 3)
			{
				// Print uncommon elements
				result = true;
				System.out.print("  " + v);
			}
		}
		if (result == false)
		{
			System.out.print("\n None  ");
		}
	}
	public static void main(String[] args)
	{
		Search task = new Search();
		int[] arr1 = {
			5 , 3 , 6 , 2 , 3 , 7
		};
		int[] arr2 = {
			2 , 4 , 5 , 8
		};
		// Get the length
		int n = arr1.length;
		int m = arr2.length;
		// Test
		task.findUncommon(arr1, arr2, n, m);
	}
}

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  4  6  7  8
// Include header file
#include <iostream>
#include <unordered_map>

using namespace std;
/*
    C++ Program
    Print uncommon elements from two arrays
*/
class Search
{
	public: void printArray(int arr[], int n)
	{
		for (int i = 0; i < n; ++i)
		{
			cout << "  " << arr[i];
		}
		cout << "\n";
	}
	void findUncommon(int arr1[], int arr2[], int n, int m)
	{
		unordered_map < int, int > record;
		bool result = false;
		for (int i = 0; i < n; ++i)
		{
			if (record.find(arr1[i]) == record.end())
			{
				// Find distinct elements in first array
				record[arr1[i]] = 1;
			}
		}
		for (int i = 0; i < m; ++i)
		{
			if (record.find(arr2[i]) == record.end())
			{
				// Find new element in second array
				record[arr2[i]] = 2;
			}
			else if (record[arr2[i]] == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record[arr2[i]] = 3;
			}
		}
		cout << "\n Array A  :";
		// Display given arrays
		this->printArray(arr1, n);
		cout << " Array B  :";
		this->printArray(arr2, m);
		cout << " Uncommon :";
		for (auto &v: record)
		{
			if (v.second != 3)
			{
				// Print uncommon elements
				result = true;
				cout << "  " << v.first;
			}
		}
		if (result == false)
		{
			cout << "\n None  ";
		}
	}
};
int main()
{
	Search *task = new Search();
	int arr1[] = {
		5 , 3 , 6 , 2 , 3 , 7
	};
	int arr2[] = {
		2 , 4 , 5 , 8
	};
	// Get the length
	int n = sizeof(arr1) / sizeof(arr1[0]);
	int m = sizeof(arr2) / sizeof(arr2[0]);
	// Test
	task->findUncommon(arr1, arr2, n, m);
	return 0;
}

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  8  4  7  3  6
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp Program
    Print uncommon elements from two arrays
*/
public class Search
{
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	public void findUncommon(int[] arr1, int[] arr2, int n, int m)
	{
		Dictionary < int, int > record = 
          new Dictionary < int, int > ();
		Boolean result = false;
		for (int i = 0; i < n; ++i)
		{
			if (!record.ContainsKey(arr1[i]))
			{
				// Find distinct elements in first array
				record.Add(arr1[i], 1);
			}
		}
		for (int i = 0; i < m; ++i)
		{
			if (!record.ContainsKey(arr2[i]))
			{
				// Find new element in second array
				record.Add(arr2[i], 2);
			}
			else if (record[arr2[i]] == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record[arr2[i]] = 3;
			}
		}
		Console.Write("\n Array A  :");
		// Display given arrays
		this.printArray(arr1, n);
		Console.Write(" Array B  :");
		this.printArray(arr2, m);
		Console.Write(" Uncommon :");
		foreach(KeyValuePair < int, int > info in record)
		{
			if (info.Value != 3)
			{
				// Print uncommon elements
				result = true;
				Console.Write("  " + info.Key);
			}
		}
		if (result == false)
		{
			Console.Write("\n None  ");
		}
	}
	public static void Main(String[] args)
	{
		Search task = new Search();
		int[] arr1 = {
			5 , 3 , 6 , 2 , 3 , 7
		};
		int[] arr2 = {
			2 , 4 , 5 , 8
		};
		// Get the length
		int n = arr1.Length;
		int m = arr2.Length;
		// Test
		task.findUncommon(arr1, arr2, n, m);
	}
}

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  6  7  4  8
package main
import "fmt"
/*
    Go Program
    Print uncommon elements from two arrays
*/

func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print("  ", arr[i])
	}
	fmt.Print("\n")
}
func findUncommon(arr1[] int, arr2[] int, n int, m int) {
	var record = make(map[int] int)
	var result bool = false
	for i := 0 ; i < n ; i++ {
		if _, found := record[arr1[i]] ; !found {
			// Find distinct elements in first array
			record[arr1[i]] = 1
		}
	}
	for i := 0 ; i < m ; i++ {
		if _, found := record[arr2[i]] ; !found {
			// Find new element in second array
			record[arr2[i]] = 2
		} else if record[arr2[i]] == 1 {
			// Common element in array a and array b
			// 3 is use to common element 
			record[arr2[i]] = 3
		}
	}
	fmt.Print("\n Array A  :")
	// Display given arrays
	printArray(arr1, n)
	fmt.Print(" Array B  :")
	printArray(arr2, m)
	fmt.Print(" Uncommon :")
	for k, v := range record {
		if v != 3 {
			// Print uncommon elements
			result = true
			fmt.Print("  ", k)
		}
	}
	if result == false {
		fmt.Print("\n None  ")
	}
}
func main() {

	var arr1 = [] int { 5, 3, 6, 2, 3, 7 }
	var arr2 = [] int { 2, 4, 5, 8 }
	// Get the length
	var n int = len(arr1)
	var m int = len(arr2)
	// Test
	findUncommon(arr1, arr2, n, m)
}

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  6  7  4  8
<?php
/*
    Php Program
    Print uncommon elements from two arrays
*/
class Search
{
	public	function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo("  ".$arr[$i]);
		}
		echo("\n");
	}
	public	function findUncommon($arr1, $arr2, $n, $m)
	{
		$record = array();
		$result = false;
		for ($i = 0; $i < $n; ++$i)
		{
			if (!array_key_exists($arr1[$i], $record))
			{
				// Find distinct elements in first array
				$record[$arr1[$i]] = 1;
			}
		}
		for ($i = 0; $i < $m; ++$i)
		{
			if (!array_key_exists($arr2[$i], $record))
			{
				// Find new element in second array
				$record[$arr2[$i]] = 2;
			}
			else if ($record[$arr2[$i]] == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				$record[$arr2[$i]] = 3;
			}
		}
		echo("\n Array A  :");
		// Display given arrays
		$this->printArray($arr1, $n);
		echo(" Array B  :");
		$this->printArray($arr2, $m);
		echo(" Uncommon :");
		foreach($record as $key => $value)
		{
			if ($value != 3)
			{
				// Print uncommon elements
				$result = true;
				echo("  ".$key);
			}
		}
		if ($result == false)
		{
			echo("\n None  ");
		}
	}
}

function main()
{
	$task = new Search();
	$arr1 = array(5, 3, 6, 2, 3, 7);
	$arr2 = array(2, 4, 5, 8);
	// Get the length
	$n = count($arr1);
	$m = count($arr2);
	// Test
	$task->findUncommon($arr1, $arr2, $n, $m);
}
main();

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  6  7  4  8
/*
    Node JS Program
    Print uncommon elements from two arrays
*/
class Search
{
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	findUncommon(arr1, arr2, n, m)
	{
		var record = new Map();
		var result = false;
		for (var i = 0; i < n; ++i)
		{
			if (!record.has(arr1[i]))
			{
				// Find distinct elements in first array
				record.set(arr1[i], 1);
			}
		}
		for (var i = 0; i < m; ++i)
		{
			if (!record.has(arr2[i]))
			{
				// Find new element in second array
				record.set(arr2[i], 2);
			}
			else if (record.get(arr2[i]) == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record.set(arr2[i], 3);
			}
		}
		process.stdout.write("\n Array A  :");
		// Display given arrays
		this.printArray(arr1, n);
		process.stdout.write(" Array B  :");
		this.printArray(arr2, m);
		process.stdout.write(" Uncommon :");
		for (let [key, value] of record)
		{
			if (value != 3)
			{
				// Print uncommon elements
				result = true;
				process.stdout.write("  " + key);
			}
		}
		if (result == false)
		{
			process.stdout.write("\n None  ");
		}
	}
}

function main()
{
	var task = new Search();
	var arr1 = [5, 3, 6, 2, 3, 7];
	var arr2 = [2, 4, 5, 8];
	// Get the length
	var n = arr1.length;
	var m = arr2.length;
	// Test
	task.findUncommon(arr1, arr2, n, m);
}
main();

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  6  7  4  8
#    Python 3 Program
#    Print uncommon elements from two arrays
class Search :
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	def findUncommon(self, arr1, arr2, n, m) :
		record = dict()
		result = False
		i = 0
		while (i < n) :
			if (not(arr1[i] in record.keys())) :
				#  Find distinct elements in first list
				record[arr1[i]] = 1
			
			i += 1
		
		i = 0
		while (i < m) :
			if (not(arr2[i] in record.keys())) :
				#  Find new element in second list
				record[arr2[i]] = 2
			elif (record.get(arr2[i]) == 1) :
				#  Common element in list a and list b
				#  3 is use to common element 
				record[arr2[i]] = 3
			
			i += 1
		
		print("\n Array A  :", end = "")
		#  Display given lists
		self.printArray(arr1, n)
		print(" Array B  :", end = "")
		self.printArray(arr2, m)
		print(" Uncommon :", end = "")
		for key, value in record.items() :
			if (value != 3) :
				#  Print uncommon elements
				result = True
				print("  ", key, end = "")
			
		
		if (result == False) :
			print("\n None  ", end = "")
		
	

def main() :
	task = Search()
	arr1 = [5, 3, 6, 2, 3, 7]
	arr2 = [2, 4, 5, 8]
	#  Get the length
	n = len(arr1)
	m = len(arr2)
	#  Test
	task.findUncommon(arr1, arr2, n, m)

if __name__ == "__main__": main()

Output

 Array A  :   5   3   6   2   3   7
 Array B  :   2   4   5   8
 Uncommon :   3   4   6   7   8
#    Ruby Program
#    Print uncommon elements from two arrays
class Search 
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	def findUncommon(arr1, arr2, n, m) 
		record = Hash.new()
		result = false
		i = 0
		while (i < n) 
			if (!record.key?(arr1[i])) 
				#  Find distinct elements in first array
				record[arr1[i]] = 1
			end

			i += 1
		end

		i = 0
		while (i < m) 
			if (!record.key?(arr2[i])) 
				#  Find new element in second array
				record[arr2[i]] = 2
			elsif (record[arr2[i]] == 1) 
				#  Common element in array a and array b
				#  3 is use to common element 
				record[arr2[i]] = 3
			end

			i += 1
		end

		print("\n Array A  :")
		#  Display given arrays
		self.printArray(arr1, n)
		print(" Array B  :")
		self.printArray(arr2, m)
		print(" Uncommon :")
		record.each { | key, value |
			if (value != 3) 
				#  Print uncommon elements
				result = true
				print("  ", key)
			end

		}
		if (result == false) 
			print("\n None  ")
		end

	end

end

def main() 
	task = Search.new()
	arr1 = [5, 3, 6, 2, 3, 7]
	arr2 = [2, 4, 5, 8]
	#  Get the length
	n = arr1.length
	m = arr2.length
	#  Test
	task.findUncommon(arr1, arr2, n, m)
end

main()

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  6  7  4  8
import scala.collection.mutable._;
/*
    Scala Program
    Print uncommon elements from two arrays
*/
class Search()
{
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	def findUncommon(arr1: Array[Int], 
      arr2: Array[Int], n: Int, m: Int): Unit = {
		var record: HashMap[Int, Int] = new HashMap[Int, Int]();
		var result: Boolean = false;
		var i: Int = 0;
		while (i < n)
		{
			if (!record.contains(arr1(i)))
			{
				// Find distinct elements in first array
				record.addOne(arr1(i), 1);
			}
			i += 1;
		}
		i = 0;
		while (i < m)
		{
			if (!record.contains(arr2(i)))
			{
				// Find new element in second array
				record.addOne(arr2(i), 2);
			}
			else if (record.get(arr2(i)).get == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record.addOne(arr2(i), 3);
			}
			i += 1;
		}
		print("\n Array A  :");
		// Display given arrays
		printArray(arr1, n);
		print(" Array B  :");
		printArray(arr2, m);
		print(" Uncommon :");
		for ((key, value) <- record)
		{
			if (value != 3)
			{
				// Print uncommon elements
				result = true;
				print("  " + key);
			}
		}
		if (result == false)
		{
			print("\n None  ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Search = new Search();
		var arr1: Array[Int] = Array(5, 3, 6, 2, 3, 7);
		var arr2: Array[Int] = Array(2, 4, 5, 8);
		// Get the length
		var n: Int = arr1.length;
		var m: Int = arr2.length;
		// Test
		task.findUncommon(arr1, arr2, n, m);
	}
}

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  4  6  7  8
import Foundation;
/*
    Swift 4 Program
    Print uncommon elements from two arrays
*/
class Search
{
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	func findUncommon(_ arr1: [Int], 
      _ arr2: [Int],
        _ n: Int, 
          _ m: Int)
	{
		var record = [Int : Int]();
		var result: Bool = false;
		var i: Int = 0;
		while (i < n)
		{
			if (!record.keys.contains(arr1[i]))
			{
				// Find distinct elements in first array
				record[arr1[i]] = 1;
			}
			i += 1;
		}
		i = 0;
		while (i < m)
		{
			if (!record.keys.contains(arr2[i]))
			{
				// Find new element in second array
				record[arr2[i]] = 2;
			}
			else if (record[arr2[i]] == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record[arr2[i]] = 3;
			}
			i += 1;
		}
		print("\n Array A  :", terminator: "");
		// Display given arrays
		self.printArray(arr1, n);
		print(" Array B  :", terminator: "");
		self.printArray(arr2, m);
		print(" Uncommon :", terminator: "");
		for (key, value) in record
		{
			if (value != 3)
			{
				// Print uncommon elements
				result = true;
				print("  ", key, terminator: "");
			}
		}
		if (result == false)
		{
			print("\n None  ", terminator: "");
		}
	}
}
func main()
{
	let task: Search = Search();
	let arr1: [Int] = [5, 3, 6, 2, 3, 7];
	let arr2: [Int] = [2, 4, 5, 8];
	// Get the length
	let n: Int = arr1.count;
	let m: Int = arr2.count;
	// Test
	task.findUncommon(arr1, arr2, n, m);
}
main();

Output

 Array A  :   5   3   6   2   3   7
 Array B  :   2   4   5   8
 Uncommon :   4   6   7   3   8
/*
    Kotlin Program
    Print uncommon elements from two arrays
*/
class Search
{
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	fun findUncommon(arr1: Array < Int > , 
                     arr2: Array < Int > , n: Int, m: Int): Unit
	{
		val record: HashMap < Int, Int > = HashMap < Int, Int > ();
		var result: Boolean = false;
		var i: Int = 0;
		while (i < n)
		{
			if (!record.containsKey(arr1[i]))
			{
				// Find distinct elements in first array
				record.put(arr1[i], 1);
			}
			i += 1;
		}
		i = 0;
		while (i < m)
		{
			if (!record.containsKey(arr2[i]))
			{
				// Find new element in second array
				record.put(arr2[i], 2);
			}
			else if (record.getValue(arr2[i]) == 1)
			{
				// Common element in array a and array b
				// 3 is use to common element 
				record.put(arr2[i], 3);
			}
			i += 1;
		}
		print("\n Array A  :");
		// Display given arrays
		this.printArray(arr1, n);
		print(" Array B  :");
		this.printArray(arr2, m);
		print(" Uncommon :");
		for ((key, value) in record)
		{
			if (value != 3)
			{
				// Print uncommon elements
				result = true;
				print("  " + key);
			}
		}
		if (result == false)
		{
			print("\n None  ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Search = Search();
	val arr1: Array < Int > = arrayOf(5, 3, 6, 2, 3, 7);
	val arr2: Array < Int > = arrayOf(2, 4, 5, 8);
	// Get the length
	val n: Int = arr1.count();
	val m: Int = arr2.count();
	// Test
	task.findUncommon(arr1, arr2, n, m);
}

Output

 Array A  :  5  3  6  2  3  7
 Array B  :  2  4  5  8
 Uncommon :  3  4  6  7  8


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