# Find all common elements in two arrays

Here given code implementation process.

``````/*
Java program
Find all common elements in two arrays
*/
import java.util.HashSet;
public class Finding
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
System.out.print("\n");
}
// Find the common distinct element which is exist in both arrays
public void commonElement(int[] arr1, int[] arr2)
{
int n1 = arr1.length;
int n2 = arr2.length;
System.out.print("\n  Array 1 :");
display(arr1, n1);
System.out.print("  Array 2 :");
display(arr2, n2);
// Use for get unique elements
HashSet < Integer > result = new HashSet < > ();
boolean status = false;
int i = 0;
for (i = 0; i < n1; ++i)
{
// Get unique values of arr1
}
System.out.print("  Common :");
// Find common elements
for (i = 0; i < n2; ++i)
{
if (result.contains(arr2[i]))
{
status = true;
System.out.print("  " + arr2[i]);
// Remove element because we need to only common distinct
result.remove(arr2[i]);
}
}
if (status == false)
{
System.out.print(" None \n");
}
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
4 , -2 , 1 , 9 , 3 , 0 , 3 , 6 , 2
};
int[] arr2 = {
1 , 4 , 23 , 6 , 23 , 1 , -2 , -1 , 9
};
//Test case
}
}``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common :  1  4  6  -2  9``````
``````// Include header file
#include <iostream>
#include <set>
using namespace std;
/*
C++ program
Find all common elements in two arrays
*/
class Finding
{
public:
//This function are displaying given array elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Find the common distinct element which is exist in both arrays
void commonElement(int arr1[], int arr2[], int n1, int n2)
{

cout << "\n  Array 1 :";
this->display(arr1, n1);
cout << "  Array 2 :";
this->display(arr2, n2);
// Use for get unique elements
set < int > result;
bool status = false;
int i = 0;
for (i = 0; i < n1; ++i)
{
result.insert(arr1[i]);
}
cout << "  Common :";
// Find common elements
for (i = 0; i < n2; ++i)
{
if (result.find(arr2[i]) != result.end())
{
status = true;
cout << "  " << arr2[i];
result.erase(arr2[i]);
}
}
if (status == false)
{
cout << " None \n";
}
}
};
int main()
{
// Define array of integer elements
int arr1[] = {
4 , -2 , 1 , 9 , 3 , 0 , 3 , 6 , 2
};
int arr2[] = {
1 , 4 , 23 , 6 , 23 , 1 , -2 , -1 , 9
};
int n1 = sizeof(arr1) / sizeof(arr1[0]);
int n2 = sizeof(arr2) / sizeof(arr2[0]);
//Test case
return 0;
}``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common :  1  4  6  -2  9``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# program
Find all common elements in two arrays
*/
public class Finding
{
//This function are displaying given array elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Find the common distinct element which is exist in both arrays
public void commonElement(int[] arr1, int[] arr2)
{
int n1 = arr1.Length;
int n2 = arr2.Length;
Console.Write("\n  Array 1 :");
display(arr1, n1);
Console.Write("  Array 2 :");
display(arr2, n2);
// Use for get unique elements
HashSet < int > result = new HashSet <int> ();
Boolean status = false;
int i = 0;
for (i = 0; i < n1; ++i)
{
}
Console.Write("  Common  :");
// Find common elements
for (i = 0; i < n2; ++i)
{
if (result.Contains(arr2[i]))
{
status = true;
Console.Write("  " + arr2[i]);
result.Remove(arr2[i]);
}
}
if (status == false)
{
Console.Write(" None \n");
}
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr1 = {
4 , -2 , 1 , 9 , 3 , 0 , 3 , 6 , 2
};
int[] arr2 = {
1 , 4 , 23 , 6 , 23 , 1 , -2 , -1 , 9
};
//Test case
}
}``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````
``````<?php
/*
Php program
Find all common elements in two arrays
*/
class Finding
{
//This function are displaying given array elements
public  function display( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
echo "\n";
}
// Find the common distinct element which is exist in both arrays
public  function commonElement( & \$arr1, & \$arr2)
{
\$n1 = count(\$arr1);
\$n2 = count(\$arr2);
echo "\n  Array 1 :";
\$this->display(\$arr1, \$n1);
echo "  Array 2 :";
\$this->display(\$arr2, \$n2);
// Use for get unique elements
\$result = array();
\$status = false;
\$i = 0;
for (\$i = 0; \$i < \$n1; ++\$i)
{
if ( in_array(\$arr1[\$i], \$result,True) == false)
{
\$result[] = \$arr1[\$i];
}
}
echo "  Common  :";
// Find common elements
for (\$i = 0; \$i < \$n2; ++\$i)
{
if ((\$key = array_search(\$arr2[\$i], \$result)) !== false)
{
\$status = true;
echo "  ". \$arr2[\$i];

unset(\$result[\$key]);
}
}
if (\$status == false)
{
echo " None \n";
}
}
}

function main()
{
// Define array of integer elements
\$arr1 = array(4, -2, 1, 9, 3, 0, 3, 6, 2);
\$arr2 = array(1, 4, 23, 6, 23, 1, -2, -1, 9);
//Test case
}
main();``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````
``````/*
Node Js program
Find all common elements in two arrays
*/
class Finding
{
//This function are displaying given array elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Find the common distinct element which is exist in both arrays
commonElement(arr1, arr2)
{
var n1 = arr1.length;
var n2 = arr2.length;
process.stdout.write("\n  Array 1 :");
this.display(arr1, n1);
process.stdout.write("  Array 2 :");
this.display(arr2, n2);
// Use for get unique elements
var result = new Set();
var status = false;
var i = 0;
for (i = 0; i < n1; ++i)
{
}
process.stdout.write("  Common  :");
// Find common elements
for (i = 0; i < n2; ++i)
{
if (result.has(arr2[i]))
{
status = true;
process.stdout.write("  " + arr2[i]);
result.delete(arr2[i]);
}
}
if (status == false)
{
process.stdout.write(" None \n");
}
}
}

function main()
{
// Define array of integer elements
var arr1 = [4, -2, 1, 9, 3, 0, 3, 6, 2];
var arr2 = [1, 4, 23, 6, 23, 1, -2, -1, 9];
//Test case
}
main();``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````
``````#   Python 3 program
#   Find all common elements in two arrays

class Finding :
# This function are displaying given list elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

#  Find the common distinct element which is exist in both lists
def commonElement(self, arr1, arr2) :
n1 = len(arr1)
n2 = len(arr2)
print("\n  Array 1 :", end = "")
self.display(arr1, n1)
print("  Array 2 :", end = "")
self.display(arr2, n2)
#  Use for get unique elements
result =set()
status = False
i = 0
while (i < n1) :
#  Get unique values of arr1
i += 1

print("  Common  :", end = "")
#  Find common elements
i = 0
while (i < n2) :
if (arr2[i] in result) :
status = True
print(" ", arr2[i], end = "")
result.remove(arr2[i])

i += 1

if (status == False) :
print(" None ")

def main() :
#  Define list of integer elements
arr1 = [4, -2, 1, 9, 3, 0, 3, 6, 2]
arr2 = [1, 4, 23, 6, 23, 1, -2, -1, 9]
# Test case

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

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````
``````#   Ruby program
#   Find all common elements in two arrays

class Finding
# This function are displaying given array elements
def display(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Find the common distinct element which is exist in both arrays
def commonElement(arr1, arr2)
n1 = arr1.length
n2 = arr2.length
print("\n  Array 1 :")
self.display(arr1, n1)
print("  Array 2 :")
self.display(arr2, n2)
#  Use for get unique elements
result = []
status = false
i = 0
while (i < n1)
result.push(arr1[i])
i += 1
end

print("  Common  :")
#  Find common elements
i = 0
while (i < n2)
if (result.include?(arr2[i]))
status = true
print("  ", arr2[i])
result.delete(arr2[i])
end

i += 1
end

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

end

end

def main()
#  Define array of integer elements
arr1 = [4, -2, 1, 9, 3, 0, 3, 6, 2]
arr2 = [1, 4, 23, 6, 23, 1, -2, -1, 9]
# Test case
end

main()``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````
``````import scala.collection.mutable._;
/*
Scala program
Find all common elements in two arrays
*/
class Finding
{
//This function are displaying given array elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Find the common distinct element which is exist in both arrays
def commonElement(arr1: Array[Int], arr2: Array[Int]): Unit = {
var n1: Int = arr1.length;
var n2: Int = arr2.length;
print("\n  Array 1 :");
this.display(arr1, n1);
print("  Array 2 :");
this.display(arr2, n2);
// Use for get unique elements
var result: Set[Int] = Set();
var status: Boolean = false;
var i: Int = 0;
while (i < n1)
{
i += 1;
}
print("  Common  :");
// Find common elements
i = 0;
while (i < n2)
{
if (result.contains(arr2(i)))
{
status = true;
print("  " + arr2(i));
result.remove(arr2(i));
}
i += 1;
}
if (status == false)
{
print(" None \n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Finding = new Finding();
// Define array of integer elements
var arr1: Array[Int] = Array(4, -2, 1, 9, 3, 0, 3, 6, 2);
var arr2: Array[Int] = Array(1, 4, 23, 6, 23, 1, -2, -1, 9);
//Test case
}
}``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````
``````/*
Swift 4 program
Find all common elements in two arrays
*/
class Finding
{
//This function are displaying given array elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Find the common distinct element which is exist in both arrays
func commonElement(_ arr1: [Int], _ arr2: [Int])
{
let n1: Int = arr1.count;
let n2: Int = arr2.count;
print("\n  Array 1 :", terminator: "");
self.display(arr1, n1);
print("  Array 2 :", terminator: "");
self.display(arr2, n2);
// Use for get unique elements
var result = Set<Int>()
var status: Bool = false;
var i: Int = 0;
while (i < n1)
{
result.insert(arr1[i]);
i += 1;
}
print("  Common  :", terminator: "");
// Find common elements
i = 0;
while (i < n2)
{
if (result.contains(arr2[i]))
{
status = true;
print("  ", arr2[i], terminator: "");
result.remove(arr2[i]);
}
i += 1;
}
if (status == false)
{
print(" None ");
}
}
}
func main()
{
// Define array of integer elements
let arr1: [Int] = [4, -2, 1, 9, 3, 0, 3, 6, 2];
let arr2: [Int] = [1, 4, 23, 6, 23, 1, -2, -1, 9];
//Test case
}
main();``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :   1   4   6   -2   9``````
``````/*
Kotlin program
Find all common elements in two arrays
*/
class Finding
{
//This function are displaying given array elements
fun display(arr: Array <Int> , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Find the common distinct element which is exist in both arrays
fun commonElement(arr1: Array < Int > , arr2: Array < Int > ): Unit
{
var n1: Int = arr1.count();
var n2: Int = arr2.count();
print("\n  Array 1 :");
this.display(arr1, n1);
print("  Array 2 :");
this.display(arr2, n2);
// Use for get unique elements
var result = mutableSetOf <Int> ();
var status: Boolean = false;
var i: Int = 0;
while (i < n1)
{
i += 1;
}
print("  Common  :");
// Find common elements
i = 0;
while (i < n2)
{
if (result.contains(arr2[i]))
{
status = true;
print("  " + arr2[i]);
result.remove(arr2[i]);
}
i += 1;
}
if (status == false)
{
print(" None \n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Define array of integer elements
var arr1: Array < Int > = arrayOf(4, -2, 1, 9, 3, 0, 3, 6, 2);
var arr2: Array < Int > = arrayOf(1, 4, 23, 6, 23, 1, -2, -1, 9);
//Test case
}``````

#### Output

``````  Array 1 :  4  -2  1  9  3  0  3  6  2
Array 2 :  1  4  23  6  23  1  -2  -1  9
Common  :  1  4  6  -2  9``````

## 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.