Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1997

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

Search the Archive

Re: How to select unique elements in a list?

  • To: mathgroup at
  • Subject: [mg7986] Re: How to select unique elements in a list?
  • From: crose at (Colin Rose)
  • Date: Wed, 30 Jul 1997 23:57:37 -0400
  • Organization: Theoretical Research Institute
  • Sender: owner-wri-mathgroup at

"Carl Woll" <carlw at> 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}

How about:

         Z[x_] := Select[ Split[Sort[x]], (Length[#] == 1)&]//Flatten

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

  In[]   Z[test]; // Timing

 Out[]   {0.05 Second,Null}          PowerMac 8600/200

  > 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 appears that your Distinct fn gives the incorrect answer, since
  (i)  Z[test] == Distinct[test] yields FALSE, and 
  (ii) Z[test] appears to be correct.
That is the problem with pattern matching: you have to catch ALL
the possible patterns.

Hope this helps



Colin Rose 
tr(I) - Theoretical Research Institute
crose at

  • Prev by Date: Re: How to select unique elements in a list?
  • Previous by thread: Re: How to select unique elements in a list?
  • Next by thread: Waiting for a user to finish with a notebook