Re: removing dublicates in lists
- To: mathgroup at smc.vnet.net
- Subject: [mg63812] Re: [mg63774] removing dublicates in lists
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Sun, 15 Jan 2006 05:43:25 -0500 (EST)
- References: <200601140732.CAA00287@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Kent Holing wrote: > removeRepetitions[L_] := Fold[If[#2==Last[#1],#1,Append[#1,#2]]&,{First[L]},Rest[L]] successfully removes dublicates in lists like {1,2,2,3,6}. > Is it an easy way to extend the functionality of this function such that it works with lists like > {{1,2,3},{44,56,78},{1,2,3},...} removing dublicate elements such that {1,2,3}? > Kent Holing > NORWAY Kent, If you don't mind having the resulting list sorted, then Union is the simplest approach (I changed your example slightly so that the sorting occurs): In[4]:= Union[{{44,56,78},{1,2,3},{1,2,3}}] Out[4]= {{1,2,3},{44,56,78}} If you don't want the resulting list sorted, then you might want to try out the following old chestnut I came up with long ago: UnsortedUnion[a_] := Block[{f}, f[i_] := (f[i] = Sequence[]; i); f /@ a] For the same example we see that duplicates are removed, but the list isn't sorted: In[5]:= UnsortedUnion[{{44,56,78},{1,2,3},{1,2,3}}] Out[5]= {{44,56,78},{1,2,3}} Carl Woll Wolfram Research
- References:
- removing dublicates in lists
- From: Kent Holing <KHO@statoil.com>
- removing dublicates in lists