Worked example
String[] first = {"a", "b", "a", "c"};
String[] second = {"b", "c", "d"};
Set<String> inFirst = new HashSet<String>();
for (int i = 0; i < first.length; i++) {
inFirst.add(first[i]);
}
Set<String> common = new HashSet<String>();
for (int i = 0; i < second.length; i++) {
if (inFirst.contains(second[i])) {
common.add(second[i]); // duplicates collapse here
}
}
System.out.println(common.size()); // 2 — b and c
Two sets working together: one answers "is it in the first?", the other collects the distinct matches.