If you read the code, and assume that @arr1 is large, without many duplicates, and that @arr2 is small without many duplicates either, then it would appear that the above will iterate over the values of @arr1 twice. Also the above has the problem that the resulting array will have no ordering relationship to the original one. I think a superior method is the one I posted in my orphaned reply. Or rather a minor variant that doesn use hash slices.
This code will only walk @arr1 and @arr2 once. Note that this is one of the weaknesses in 'big O notation'. Both of these solutions are O(N), however, the worst cases of both are 2(N+M) and N+M, thus the later has a much better worst case even though they are the same order of solution.my %remove; $remove{$_}++ for @arr2; @clean=grep !$remove{$_},@arr1;
BrowserUk posted a benchmark that I will mod to benchmark the various solutions, including the one above.
Regards,
--- demerphq
my friends call me, usually because I'm late....
In reply to Re: Re: How to splice out multiple entries from an array
by demerphq
in thread How to splice out multiple entries from an array
by Ya
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |