my %hits; ++$hits{$_} for @a1; my @matched = grep {exists $hits{$_}} @a2;