Re: Intersection and element counts
- To: mathgroup at smc.vnet.net
- Subject: [mg20673] Re: [mg20615] Intersection and element counts
- From: BobHanlon at aol.com
- Date: Sun, 7 Nov 1999 02:10:08 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Here is an approach using a variation of run length encoding: runs[aList_List?VectorQ] := {First[#], Length[#]} & /@ Split[Sort[aList]]; myIntersection[list1_List?VectorQ, list2_List?VectorQ] := Module[ {intr = Intersection[list1, list2]}, Flatten[ Cases[Transpose[{Select[runs[list1], MemberQ[intr, #[[1]]] &], Select[runs[list2], MemberQ[intr, #[[1]]] &]}], {{x_, m_Integer}, {x_, n_Integer}} :> Table[x, {Min[m, n]}]]]]; myIntersection[{a, a, a, b, b, c}, {a, a, b, b}] {a, a, b, b} Bob Hanlon In a message dated 11/4/1999 6:37:33 AM, acus at itpa.lt writes: >I am interesting in intersecion, which takes into >account the number of the same elements. > >It is I would like > >myIntersection[{a,a,a,b,b,c},{a,a,b,b}] > >to give me {a,a,b,b}. > >Any solutions? >Do theoretical speed of this function is >very different of usual intersection algorithm speed? >