MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to select unique elements in a list?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg7992] Re: How to select unique elements in a list?
  • From: Paul Abbott <paul at physics.uwa.edu.au>
  • Date: Wed, 30 Jul 1997 23:57:44 -0400
  • Organization: University of Western Australia
  • Sender: owner-wri-mathgroup at wolfram.com

C. Woll wrote:

> First, let me say that Union does not do what I'm looking for! I am
> interested in a function which returns the unique elements from a list.
> Let's call the function Distinct. Then, I want
> 
> Distinct[{i1,i2,i3,i2}]
> 
> to return
> 
> {i1,i3}
> 
> Of course, I can write a function to do this, but I am hoping someone can
> do a better job them my feeble attempt. So, my best functional solution so
> far is,
> 
> RemoveDuplicates[a___,i_,i_,b___]:=RemoveDuplicates[a,b]
> Distinct[a_List]:=List@@RemoveDuplicates@@Sort[a]
> 
> However, this solution is very slow. For example, with
> 
> test=Table[i[Random[Integer,1000]],{1000}];
> 
> it takes approximately 27 seconds to evaluate Distinct[test] on an SGI
> system. This is slow when compared to .01 seconds to evaluate Union[test]!
> 
> It's likely that the way to go is to use a procedural method instead of a
> functional method.
> 
> At any rate, can anyone come up with a solution which is competitive in
> speed with Union?

Here is a solution using Split:

 In[1]:= Distinct[a_List] :=  Select[Split[Sort[a]], Length[#] == 1 & ]

Check that it works:

 In[2]:= l = {i1, i2, i3, i2};

 In[3]:= Distinct[l]
 Out[3]= {{i1}, {i3}}

 In[4]:= test = Table[i[Random[Integer, 1000]],{1000}];

 In[5]:= Timing[Distinct[test];]
 Out[5]= {0.1 Second, Null}

Comparison:

 In[6]:= RemoveDuplicates[a___, i_, i_, b___] := RemoveDuplicates[a, b]

 In[7]:= Distinct[a_List] := List @@ RemoveDuplicates @@ Sort[a]

 In[8]:= Timing[Distinct[test]; ]
 Out[8]= {30.4667 Second, Null}

Cheers,
	Paul 
 
____________________________________________________________________ 
Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia           
Nedlands WA  6907                     mailto:paul at physics.uwa.edu.au 
AUSTRALIA                              http://www.pd.uwa.edu.au/Paul

            God IS a weakly left-handed dice player
____________________________________________________________________


  • Prev by Date: Re: Q: implementation of Dot
  • Next by Date: Printing problems
  • Previous by thread: Re: How to select unique elements in a list?
  • Next by thread: Re: How to select unique elements in a list?