my %wanted = map { $_, 1 } @array_1; my $bag = Set::Bag->new( ); foreach( @array_2 ) { $bag->insert( $_ => 1 ) if $wanted{ $_ }; }