Uhh ... you do realize that you just implemented a very rudimentary hash table in Perl using ... wait for it ... hashes! Except, you don't handle collisions. What's wrong with:
sub intersection {
my ( $list1, $list2 ) = @_;
my %hash;
@hash{@$list1} = undef;
my @intersection = grep { exists $hash{$_} } @$list2;
return @intersection
}
Update: Fixed mistake in my code (map -> grep)
Compare mine to yours and I think you'll notice a difference.
My criteria for good software:
- Does it work?
- Can someone else come in, make a change, and be reasonably certain no bugs were introduced?