int[] a1 = arg1.expand();
int[] a2 = arg2.expand();
IntToIntMap map = new IntToIntHashMap(a1.length, 0.5);
// allow plenty of free space, it's better for lookups (though worse for iteration)
for (int i=0; i<a1.length; i++) {
if (map.find(a1[i])) {
// no action: duplicate
} else {
map.put(a1[i], (i>a2.length-1 ? -1 : a2[i]));
}
}