This is O(N*M) (where N is sizeof A, and M is sizeof B). You can do it in O(N+M) by building a hash with the contents of B, and using that for your test (instead of grep).
my %B = map { $_ => 1 } @B;
foreach my $data ( @A ) {
push @A_not_B, $data unless $B{$data};
}
You could also replace the foreach loop with:
@A_not_B = grep { ! $B{$_} } @A;
If N and M are large, the difference between O(N+M) and O(N*M) can be substantial. For the sample set, it is probably not noticeable.
It is said that "only perl can parse Perl." I don't even come close until my 3rd cup of coffee. --MidLifeXis
|