Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Code requested for array intersection count

by sauoq (Abbot)
on Jun 09, 2003 at 15:43 UTC ( #264361=note: print w/replies, xml ) Need Help??


in reply to Code requested for array intersection count

As you don't care how many times the items are in array 1, I'd use a hash rather than multiple greps. A hash will make it an O(M + N) algorithm, the greps are an O(M x N) solution. Of course, this will use more memory too and you should take that into consideration when choosing which to use.

my %seen; @seen{@array_1} = (0) x @array_1; exists( $seen{$_} ) and $seen{$_}++ for @array_2;

-sauoq
"My two cents aren't worth a dime.";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://264361]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2021-09-22 03:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?