Re: Extract values and multilpicities from list

• To: mathgroup at smc.vnet.net
• Subject: [mg65053] Re: Extract values and multilpicities from list
• From: "Dr. Wolfgang Hintze" <weh at snafu.de>
• Date: Sun, 12 Mar 2006 23:59:14 -0500 (EST)
• References: <due96k\$ach\$1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Thanks to everybody in the group who sent replies to my question.
Amazing number, and variety as well!

Here is an overview over the different types of solution proposals

1) Type Union-Count (3 nominations)
{#,Count[lstIn,#]}&/@Union@lstIn

2) Type Split-Sort (10 nominations)
This is mainly run-length-encoding which also appears in the Help of mma
{#[[1]],Length[#]}&/@Split@Sort@lstIn

3) Type Reap-Sow (1 nomination)
fun[x_List]:=Reap[Sow[1,x],_,{#1,Length[#2]}&][[2]]//Sort;

4) Type Frequency (1 nomination)
<< Statistics`DataManipulation`
lstOut = Reverse /@ Frequencies[lstIn]//Timing;

5) Type Sparse-Array (1 nomination)
runs[d_] :=
SparseArray[Transpose[{d, Range[Length[d]]}] -> Table[1, {Length[d]}]] /.
SparseArray[_, _, _, {_, {p_, __}, _}] :>
Transpose[{Range[Length[p] - 1], Rest[p] - Most[p]}]

I was not happy with the heading of my formulation of the problem
(disregarding the little typo ;-)). At first I tended to call it
something like "histogram problem", now I think that "run-length
encoding" is the most apropriate.

Best regards,
Wolfgang

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
>
>

```

• Prev by Date: Re: 3D-plot over a triangle
• Next by Date: Re: Graphics3D polygon face colors
• Previous by thread: Re: Extract values and multilpicities from list