Re: Generalizing Complement to handle multiple occurrences
- To: mathgroup at smc.vnet.net
- Subject: [mg112450] Re: Generalizing Complement to handle multiple occurrences
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Thu, 16 Sep 2010 05:58:14 -0400 (EDT)
Hi Mark, This is a follow-up to my previous post. There is a much simpler method than the one I posted before, but with somewhat different performance characteristics: Clear[unsortedComplement]; unsortedComplement[x_, y_] := x /. Dispatch[Thread[Union[y] -> Sequence[]]]; This one can be either somewhat (up to 2-3 times) faster or somewhat (up to 2-3 times) slower than the previous one, depending on the fraction of deleted elements in a first list and on the size of the (union of) the second list. It will likely be faster if the more significant part of the list is deleted, or if the second list gets smaller, and slower otherwise. Regards, Leonid On Wed, Sep 15, 2010 at 12:38 PM, Mark Coleman <markspcoleman at gmail.com>wrote: > Greetings, > > I'm wondering how one can efficiently generalize the built-in > Complement function to return multiple occurrences of elements. For > instance, the current version generates > > a={1,2,2,3,4,4,4,4} > b={3,5,7} > > Complement[a,b]={1,2,4} > > I'm looking for a way to define > > myComplement[a,b]={1,2,2,4,4,4,4} > > My current code is very inefficient! > > Thanks, > > Mark > >