# you have @arr1 and @arr2 my %lookup = map { $_ => undef } @arr1; my @common = grep { exists $lookup{$_} } @arr2;