MathGroup Archive 2006

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

Search the Archive

Re: Extract values and multilpicities from list

  • To: mathgroup at smc.vnet.net
  • Subject: [mg64897] Re: [mg64849] Extract values and multilpicities from list
  • From: "Carl K. Woll" <carlw at wolfram.com>
  • Date: Tue, 7 Mar 2006 06:11:47 -0500 (EST)
  • References: <200603050819.DAA09793@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Dr. Wolfgang Hintze wrote:
> Given a list of integers which may repeat, e.g.
> 
> lstIn = {2,3,4,4,2,1,1,5,4}
> 
> provide a list of different values and their respective multiplicities,
> in the example,
> 
> LstOut= {{1,2},{2,2},{3,1},{4,3},{5,1}}
> 
> Who finds the shortest function doing this task in general?
> 
> Thanks.
> 
> Best regards,
> Wolfgang

For the special case where the integers are positive and not too large, 
then the following approach is probably close to the fastest.

runs[d_] :=
  SparseArray[Transpose[{d, Range[Length[d]]}] -> Table[1, {Length[d]}]] /.
   SparseArray[_, _, _, {_, {p_, __}, _}] :>
    Transpose[{Range[Length[p] - 1], Rest[p] - Most[p]}]

The simplest solution is probably:

runs2[d_] := {First[#], Length[#]} & /@ Split[Sort[d]]

The output of runs will include integers with 0 length runs, so we need 
to eliminate them when comparing results:

In[22]:=
r1=runs[data];//Timing
r2=runs2[data];//Timing
DeleteCases[r1,{_,0}]===r2

Out[22]=
{0.485 Second,Null}

Out[23]=
{0.781 Second,Null}

Out[24]=
True

Carl Woll
Wolfram Research


  • Prev by Date: Re: MatrixForm to JPG
  • Next by Date: Re: MatrixForm to JPG
  • Previous by thread: Re: Extract values and multilpicities from list
  • Next by thread: Re: Extract values and multilpicities from list