MathGroup Archive 2010

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

Search the Archive

Re: Occurrences in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106374] Re: Occurrences in Mathematica
  • From: Peter Pein <petsie at dordos.net>
  • Date: Mon, 11 Jan 2010 05:28:16 -0500 (EST)
  • References: <hic33o$59f$1@smc.vnet.net>

George schrieb:
> Hi,
> 
> 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?
> 
> Thks much
> George
> 

Hi George,

calculating some data:

SeedRandom[5];
theList=Insert[RandomChoice[#/Tr[#]&[1/#^4]->#,{10^6}]&[Range[100]],{a,b,a,c},123456];

to preserve the order of occurrance, I would use:

Reap[
   NestWhile[
     (If[FreeQ[##1],
       Sow[#2];#1,
       DeleteCases[##1]]&)@@{Rest[#1],First[#1]}&,
     theList,
     #1=!={}&]
][[2,1]]

which gives: {26,29,35,{a,b,a,c},40,60,28,34,33,31,32}

If you want sorted results, use the simple (and fast)

Cases[Split[Sort[theList]],{x_}:>x]

---> {26,28,29,31,32,33,34,35,40,60,{a,b,a,c}}


HTH,
Peter


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