my (@intersection, @union); my %count; @count{@array1} = undef; for (@array2) { if (exists $count{$_}) { push @union, $_; } else { push @intersection, $_; } }