my %set1 = map { $_ => 1 } @array1; my %set2 = map { $_ => 1 } @array2; my %intersection = map { $_ => 1 } grep $set1{$_}, keys %set2;