0

So here is the case:

We have 5 users. User A, B, C, D, and E. And user A have three array like this:

Array I : A, B, C

Array II : A, B

Array III : A

So, Who is the users that are inside all of that array? (The answer should be user A because user A is inside all of that array) but how to check that in java?

I'm not sure what is the best title of this question

Iceka
  • 17
  • 6

1 Answers1

0

Add all string arrays to sets, then find the intersection of all sets:

String[] array1 = {"A", "B", "C"};
String[] array2 = {"A", "B"};
String[] array3 = {"A"};
Set<String> s1 = new HashSet<>(Arrays.asList(array1));
Set<String> s2 = new HashSet<>(Arrays.asList(array2));
Set<String> s3 = new HashSet<>(Arrays.asList(array3));

// now take the intersection of s1 and s2, followed by s1 and s3
s1.retainAll(s2);
s1.retainAll(s3);

for (String item : s1) {
    System.out.println("common item: " + item);
}

This prints:

common item: A
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360