MathGroup Archive 1997

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

Search the Archive

Re: Re: How to select unique elements in a list?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg8434] Re: [mg8348] Re: How to select unique elements in a list?
  • From: Joze Korelc <jkorelc at gringo.fagg.uni-lj.si>
  • Date: Sat, 30 Aug 1997 00:42:43 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

Nigel King wrote:
> 
> In article <5rmq5u$po1 at smc.vnet.net>,
> crose at c2.telstra-mm.net.au (Colin Rose) wrote:
> 
> >"Carl Woll" <carlw at u.washington.edu> 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}
> >
> >  RemoveDuplicates[a___,i_,i_,b___]:=RemoveDuplicates[a,b]
> >  Distinct[a_List]:=List@@RemoveDuplicates@@Sort[a]
> 
> I am not sure that people have noticed in this that the removal above does
> not remove odd numbers of duplicates
> 
> In[19]:=Distinct[{1,2,2,2,3}]
> Out[19]={1,2,3}
> 
> any solutions? As noticed pattern matching is not easy!
> 
> Nigel King
> Northern Telecom

Here is a nice and fast solution ...

Distinct=Cases[Split[Sort[#]],{i_}:>i]&

Joze Korelc


========================================================================
Joze Korelc

University of Ljubljana                  Institut of Mechanics 4
Faculty of Civil Engng. and Geodesy      Hochschulstrasse 1
Jamova 2, SI-1000,Ljubljana              DE-64289 Darmstadt
Slovenia                                 Germany

Phone:+386 61-1768623 Fax: +386 61-1250681
E-Mail: jkorelc at gringo.fagg.uni-lj.si


  • Prev by Date: Re: TeXForm for matrices?
  • Next by Date: Re: Partition List
  • Previous by thread: Re: Re: How to select unique elements in a list?
  • Next by thread: Polynomial Canonical Form