RE: Keeping order with Union
- To: mathgroup at smc.vnet.net
- Subject: [mg32611] RE: [mg32590] Keeping order with Union
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 31 Jan 2002 01:45:42 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Dave,
If your list is composed of integers or exact expressions, then there is the
very efficient and clever routine that Carl Woll put forth a year or so ago
on MathGroup.
RemoveRepeats[x_] :=
Block[{i},
i[n_] := (i[n] = Sequence[]; n);
i /@ x]
list = Array[Random[Integer, {0, 9}] &, 100]
{1,9,0,7,6,4,3,1,7,6,2,3,0,1,7,7,0,1,0,9,1,1,5,4,5,0,7,6,7,9,2,4,8,3,6,2,1,2
,\
0,8,7,7,0,7,6,0,8,1,8,0,7,2,0,2,2,8,8,2,4,9,6,0,2,9,6,3,7,6,8,3,4,0,8,6,9,4,
7,\
3,4,8,5,0,3,6,2,0,6,6,8,0,8,2,9,3,9,6,7,0,0,8}
RemoveRepeats[list]
{1, 9, 0, 7, 6, 4, 3, 2, 5, 8}
Here is an alternative method, much slower, but it can be used on
approximate expressions if you adjust the SameTest to something appropriate
for the specific case.
list2 = Transpose[{Range[Length[list]], list}];
list2 = Union[list2, SameTest ->
(#1[[2]] == #2[[2]] & )];
Last /@ list2
When I ran on a list of 100000 integers, the Woll routine took 0.39 seconds,
and the second routine took 6.42 seconds.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
> From: Dave Snead [mailto:dsnead6 at charter.net]
To: mathgroup at smc.vnet.net
>
> I'd like a function that throws out duplicate entries in a list and keeps
> the
> original list order. (when it sees 2 equal entries it throws out the one
> that is later in the list). I'd use Union, but it sometimes changes the
> order of the elements in the list. What's the tersest way to do this?
> Thanks in advance,
> Dave Snead
>