• # You have three Arrays…

Akash  updated 1 year, 10 months ago 2 Members · 2 Posts
• ### Akash

at

You have three Arrays.

A = {2, 5, 3, 2, 8,1}

B = {7, 9, 5, 2, 4, 10, 10}

C = {6, 7, 5, 5, 3, 7}

make an array from this three arrays which elements is present in at least two array.

This question was followed by instead of three arrays. If you have a list of array then what will be the solution? Also what will be the time complexity?

• ### Akash

at

package myProject;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;

public class Stores {

public static void main(String[] args) {

Integer[] arr1 = {2, 5, 3, 2, 8,1};

Integer[] arr2 = {7, 9, 5, 2, 4, 10, 10};

Integer[] arr3 = {6, 7, 5, 5, 3, 7};

Set<Integer> set1 = new HashSet<>(Arrays.asList(arr1));

Set<Integer> set2 = new HashSet<>(Arrays.asList(arr2));

Set<Integer> set3 = new HashSet<>(Arrays.asList(arr3));

Set<Integer> set = new HashSet<>();

ArrayList<Integer> list = new ArrayList<>();

for (Integer val: set) {

if(set1.contains(val) && set2.contains(val) && set3.contains(val)) {

}

else if(set1.contains(val) && set2.contains(val)) {

}

else if(set2.contains(val) && set3.contains(val)) {

}

else if(set1.contains(val) && set3.contains(val)) {

}

}

System.out.println(list);

}}

Viewing 1 - 2 of 2 replies