Re: easiest way to sort a list?
- To: mathgroup at smc.vnet.net
- Subject: [mg18320] Re: [mg18308] easiest way to sort a list?
- From: "ARNOLDK" <arnoldk at gauss.cam.wits.ac.za>
- Date: Sat, 26 Jun 1999 19:08:01 -0400
- Sender: owner-wri-mathgroup at wolfram.com
>Hi everyone,
>
>Here's my question: Suppose you have a list of integers, not all distinct,
>say
> {1, 5, 3, 5, 10, 10, 1},
>
>and you want to obtain the list
>
> {1, 5, 3, 10}
>
>which is the set of distinct elements but with the order preserved.
Here's one way
In[5]:=
{1, 5, 3, 5, 10, 10, 1,4,4}
//.{a___,b_,c___,b_,d___}->{a,b,c,d}
Out[5]=
{1,5,3,10,4}
Or try this
In[20]:=
union2[l_]:=Fold[Flatten[If[!MemberQ[#1,#2],Append[#1,#2],#1]]&,{},l
]
In[21]:=
union2[{1, 5, 3, 5, 10, 10, 1,4,4}]
Out[21]=
{1,5,3,10,4}
Arnold Knopfmacher
Wits University
South Africa