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++
#include <bits/stdc++.h>
using namespace std;
void get(vector< int >& p, vector< int >& c, vector< int >& m)
{
set< int > s1;
set< int > s2;
set< int > s3;
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);
}
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 << " " ;
}
}
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;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void get(
int [] p, int [] c, int [] m)
{
Set<Integer> s1 = new HashSet<>();
Set<Integer> s2 = new HashSet<>();
Set<Integer> s3 = new HashSet<>();
Set<Integer> 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);
}
List<Integer> 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 + " " );
}
}
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
def get(p, c, m) :
s1 = set ();
s2 = set ();
s3 = set ();
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);
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 = " " );
if __name__ = = "__main__" :
arr = [ 1 , 1 , 3 , 2 , 4 ];
brr = [ 2 , 3 , 5 ];
crr = [ 3 , 6 ];
get(arr, brr, crr);
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
static void get (
int [] p, int [] c, int [] m)
{
HashSet< int > s1 = new HashSet< int >();
HashSet< int > s2 = new HashSet< int >();
HashSet< int > s3 = new HashSet< int >();
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);
}
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 + " " );
}
}
public static void Main()
{
int [] arr = { 1, 1, 3, 2, 4 };
int [] brr = { 2, 3, 5 };
int [] crr = { 3, 6 };
get (arr, brr, crr);
}
}
|
Javascript
<script>
function get(p, c, m)
{
let s1 = new Set();
let s2 = new Set();
let s3 = new Set();
let set = new Set();
for (i of p) {
s1.add(i);
set.add(i);
}
for (i of c) {
s2.add(i);
set.add(i);
}
for (i of m) {
s3.add(i);
set.add(i);
}
let result = [];
for (i of [...set].reverse()) {
if (s1.has(i) && s2.has(i)
|| s2.has(i) && s3.has(i)
|| s1.has(i) && s3.has(i))
document.write(i + " " );
}
}
let arr = [1, 1, 3, 2, 4];
let brr = [2, 3, 5];
let crr = [3, 6];
get(arr, brr, crr);
</script>
|
Time Complexity: O(n1+n2+n3) (where n1, n2 and n3 are the length of given arrays)
Auxiliary Space: O(n1+n2+n3)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...