If [x,y], [x,z] should give [y,z] here what I suggest:
String[] a = {"a","b","c","d"}; String[] b = {"b", "c", "x"}; Set<String> set = new HashSet<>(Arrays.asList(a)); for (String s : new HashSet<>(Arrays.asList(b)) { if (!set.add(s))
If, on the other hand, [x,y], [x,z] should give [y] , I would suggest
Set<String> set = new HashSet<>(Arrays.asList(a)); set.removeAll(Arrays.asList(b));
source share