Re: Occurrences in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg106376] Re: Occurrences in Mathematica
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Mon, 11 Jan 2010 05:28:40 -0500 (EST)
On 1/10/10 at 3:28 AM, gtatishvili at gmail.com (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}}