MathGroup Archive 2010

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

Search the Archive

Re: Occurrences in Mathematica

On 1/10/10 at 3:28 AM, gtatishvili at (George) wrote:

>I have a very big list of elements and I need Mathematica to give me
>only those elements which occure only once...

>Could anybody advise me please how to write the code for that?

Here are a couple of ways. First I generate a short list likely
to have both unique items and items that are duplicated

In[2]:= list = RandomInteger[10, {20}]

Out[2]= {6,8,10,8,9,6,6,2,2,3,4,1,4,6,1,0,10,7,8,0}

Then, I extract the unique items by first sorting them then
using Split to group them. In this particular case, I take
advantage of knowing the items in the list are integers and use
that information when specifying the pattern for Cases

In[3]:= Cases[Split[Sort@list], {_Integer}]

Out[3]= {{3}, {7}, {9}}

Here is a little more general method which selects items of
length 1.

In[4]:= Select[Split[Sort@list], Length@# == 1 &]

Out[4]= {{3}, {7}, {9}}

  • Prev by Date: Re: Occurrences in Mathematica
  • Next by Date: Re: Radicals simplify
  • Previous by thread: Re: Occurrences in Mathematica
  • Next by thread: Re: Occurrences in Mathematica