Re: Keepping order with Union
- To: mathgroup at smc.vnet.net
- Subject: [mg32655] Re: Keepping order with Union
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Fri, 1 Feb 2002 16:11:03 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Dave Snead wanted a function that throws out duplicate elements in a list and keeps the original order of the elements that remain. There was a long thread on this subject about two years ago and much of it is summarized at http://library.wolfram.com/mathgroup/archive/2000/Jun/msg00115.html Many agree that the best way to do this is with the following ingenious function written by Carl Woll. -------- DeleteRepetitions[X_]:=Block[{t}, t[n_]:=(t[n]=Sequence[];n);t/@X ]; --------- Example: In[]:= DeleteRepetitions[ {3,1,1,6,3,2,1,1,8,2,6,8} ] Out= {3,1,6,2,8} ------------- That's one of the best Mathematica tricks I have seen. Regards, Ted Ersek Get Mathematica tips, tricks from http://www.verbeia.com/mathematica/tips/Tricks.html