# Find numbers present in at least two of the three arrays

• Last Updated : 22 Nov, 2021

Given 3 arrays, arr[], brr[], and crr[], the task is to find the common elements in at least 2 arrays out of the given 3 arrays
Examples:

Input: arr[] = {1, 1, 3, 2, 4}, brr[] = {2, 3, 5}, crr[] = {3, 6}
Output: {2, 3}
Explanation: Elements 2 and 3 appear in atleast 2 arrays

Input: arr[] = {3, 1}, brr[] = {2, 3}, crr[] = {1, 2}
Output: {2, 3, 1}

Approach: The task can be solved with the help of HashSet Follow the below steps to solve the problem:

• Create three hashsets (s1, s2, and s3) to store distinct elements of the three arrays and also one HashSet to store distinct elements of all three arrays.
• Iterate in set and check for the common elements in atleast two sets(s1, s2), (s1, s3) and (s2, s3).
• If an element satisfies the above condition, print that element.

Below is the implementation of the above code:

## C++

 `// C++ implementation of above approach``#include ``using` `namespace` `std;` `void` `get(vector<``int``>& p, vector<``int``>& c, vector<``int``>& m)``{` `    ``// Store distinct elements of array arr[]``    ``set<``int``> s1;` `    ``// Store distinct elements of array brr[]``    ``set<``int``> s2;` `    ``// Store distinct elements of array crr[]``    ``set<``int``> s3;` `    ``// Store the distinct elements``    ``// of all three arrays``    ``set<``int``> set;``    ``for` `(``auto` `i : p) {``        ``s1.insert(i);``        ``set.insert(i);``    ``}` `    ``for` `(``auto` `i : c) {``        ``s2.insert(i);``        ``set.insert(i);``    ``}` `    ``for` `(``int` `i : m) {``        ``s3.insert(i);``        ``set.insert(i);``    ``}` `    ``// Stores the common elements``    ``vector<``int``> result;``    ``for` `(``auto` `i : set) {``        ``if` `(s1.count(i) && s2.count(i)``            ``|| s2.count(i) && s3.count(i)``            ``|| s1.count(i) && s3.count(i))``            ``cout << i << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``vector<``int``> arr = { 1, 1, 3, 2, 4 };``    ``vector<``int``> brr = { 2, 3, 5 };``    ``vector<``int``> crr = { 3, 6 };``    ``get(arr, brr, crr);` `    ``return` `0;``}` `    ``// This code is contributed by rakeshsahni`

## Java

 `// Java implementation of above approach` `import` `java.io.*;``import` `java.util.*;` `class` `GFG {``    ``public` `static` `void` `get(``        ``int``[] p, ``int``[] c, ``int``[] m)``    ``{` `        ``// Store distinct elements of array arr[]``        ``Set s1 = ``new` `HashSet<>();` `        ``// Store distinct elements of array brr[]``        ``Set s2 = ``new` `HashSet<>();` `        ``// Store distinct elements of array crr[]``        ``Set s3 = ``new` `HashSet<>();` `        ``// Store the distinct elements``        ``// of all three arrays``        ``Set set = ``new` `HashSet<>();``        ``for` `(``int` `i : p) {``            ``s1.add(i);``            ``set.add(i);``        ``}` `        ``for` `(``int` `i : c) {``            ``s2.add(i);``            ``set.add(i);``        ``}` `        ``for` `(``int` `i : m) {``            ``s3.add(i);``            ``set.add(i);``        ``}` `        ``// Stores the common elements``        ``List result``            ``= ``new` `ArrayList<>();``        ``for` `(``int` `i : set) {``            ``if` `(s1.contains(i)``                    ``&& s2.contains(i)``                ``|| s2.contains(i)``                       ``&& s3.contains(i)``                ``|| s1.contains(i)``                       ``&& s3.contains(i))``                ``System.out.print(i + ``" "``);``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] arr = { ``1``, ``1``, ``3``, ``2``, ``4` `};``        ``int``[] brr = { ``2``, ``3``, ``5` `};``        ``int``[] crr = { ``3``, ``6` `};``        ``get(arr, brr, crr);``    ``}``}`

## Python3

 `# Python3 implementation of above approach``def` `get(p, c, m) :` `    ``# Store distinct elements of array arr[]``    ``s1 ``=` `set``();` `    ``# Store distinct elements of array brr[]``    ``s2 ``=` `set``();` `    ``# Store distinct elements of array crr[]``    ``s3 ``=` `set``();` `    ``# Store the distinct elements``    ``# of all three arrays``    ``set_obj ``=` `set``();``    ` `    ``for` `i ``in` `p :``        ``s1.add(i);``        ``set_obj.add(i);` `    ``for` `i ``in` `c :``        ``s2.add(i);``        ``set_obj.add(i);` `    ``for` `i ``in` `m :``        ``s3.add(i);``        ``set_obj.add(i);` `    ``# Stores the common elements``    ``result ``=` `[];``    ``for` `i ``in` `set_obj :``        ``if` `(``list``(s1).count(i) ``and` `list``(s2).count(i)``            ``or` `list``(s2).count(i) ``and` `list``(s3).count(i)``            ``or` `list``(s1).count(i) ``and` `list``(s3).count(i)) :``            ``print``(i,end``=` `" "``);``  ` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``1``, ``1``, ``3``, ``2``, ``4` `];``    ``brr ``=` `[ ``2``, ``3``, ``5` `];``    ``crr ``=` `[ ``3``, ``6` `];``    ``get(arr, brr, crr);` `    ``# This code is contributed by AnkThon`

## C#

 `// C# implementation of above approach``using` `System;``using` `System.Collections;``using` `System.Collections.Generic;` `class` `GFG {``    ``static` `void` `get``(``        ``int``[] p, ``int``[] c, ``int``[] m)``    ``{` `        ``// Store distinct elements of array arr[]``        ``HashSet<``int``> s1 = ``new` `HashSet<``int``>();` `        ``// Store distinct elements of array brr[]``        ``HashSet<``int``> s2 = ``new` `HashSet<``int``>();` `        ``// Store distinct elements of array crr[]``        ``HashSet<``int``> s3 = ``new` `HashSet<``int``>();` `        ``// Store the distinct elements``        ``// of all three arrays``        ``HashSet<``int``> ``set` `= ``new` `HashSet<``int``>();``        ``foreach` `(``int` `i ``in` `p) {``            ``s1.Add(i);``            ``set``.Add(i);``        ``}` `        ``foreach` `(``int` `i ``in` `c) {``            ``s2.Add(i);``            ``set``.Add(i);``        ``}` `        ``foreach` `(``int` `i ``in` `m) {``            ``s3.Add(i);``            ``set``.Add(i);``        ``}` `        ``// Stores the common elements``        ``ArrayList result``            ``= ``new` `ArrayList();``        ``foreach` `(``int` `i ``in` `set``) {``            ``if` `(s1.Contains(i)``                    ``&& s2.Contains(i)``                ``|| s2.Contains(i)``                       ``&& s3.Contains(i)``                ``|| s1.Contains(i)``                       ``&& s3.Contains(i))``                ``Console.Write(i + ``" "``);``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main()``    ``{``        ``int``[] arr = { 1, 1, 3, 2, 4 };``        ``int``[] brr = { 2, 3, 5 };``        ``int``[] crr = { 3, 6 };``        ``get``(arr, brr, crr);``    ``}``}` `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`2 3 `

Time Complexity: O(n1+n2+n3)  (where n1, n2 and n3  are the length of given arrays)
Auxiliary Space: O(n1+n2+n3)

My Personal Notes arrow_drop_up