sub deDuppedArray { my ($a_ref, $b_ref, $my_bonly, $my_isec_plus_bonly) = @_; # reference my (%Aseen, %Bseen, %count, @isec, @diff, @union, @aonly) = (); @Aseen{@$a_ref} = (); # lookup table @Bseen{@$b_ref} = (); # lookup table foreach my $e (@$a_ref, @$b_ref) { $count{$e}++ } # put all items in hash table foreach my $e (keys %count) { # interate over each key of hash table push(@union, $e); # keys of hash table = union if ($count{$e} == 2) { push @isec, $e; # seen more than once = intersection } else { push(@aonly, $e) unless exists $Bseen{$e}; # seen once + from A = Aonly push(@$my_bonly, $e) unless exists $Aseen{$e}; # seen once + from A = Aonly } } @$my_isec_plus_bonly = ( @isec, @$my_bonly); }