my @array1 = qw(foo foo bar baz); my @array2 = qw(bar grmpf asdf); my (@intersection, @union); my %count; @count{@array1} = undef; for (@array2) { if (exists $count{$_}) { push @union, $_; } else { push @intersection, $_; } } use Data::Dumper; print Dumper \@intersection, \@union; __END__ $VAR1 = [ 'grmpf', 'asdf' ]; $VAR2 = [ 'bar' ];