MathGroup Archive 2010

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

Search the Archive

Re: List of multiple elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112085] Re: List of multiple elements
  • From: Dana DeLouis <dana.del at gmail.com>
  • Date: Sun, 29 Aug 2010 02:51:38 -0400 (EDT)

> Given the list
> a={1,1,2,1,2};
> ... how to compute the list of multiple elements.
> {1,1,2}

Hi.  One additional way might be to just delete each unique item from the original list.

DupsOnly[v_List] := Module[{u, t},
  u = Union[v];
  t = Fold[DeleteCases[#1, #2, {1}, 1] &, v, u];
  Sort[t]
  ]

DupsOnly[{1, 1, 2, 1, 2}]
{1, 1, 2}

v = RandomChoice[Range[5], 10]
{4, 2, 2, 3, 5, 5, 3, 3, 1, 1}

Sort[v]
{1, 1, 2, 2, 3, 3, 3, 4, 5, 5}

DupsOnly[v]
{1, 2, 3, 3, 5}


I may be wrong, but the suggestion on =93DuplicateElements=94 seems a little slower in the following case:

v = RandomChoice[Range[5], 10000];

Timing[DupsOnly[v]] // First
0.003875

Timing[DuplicateElements[v]] // First
0.5468

= = = = = = = = = =
HTH  : >)
Dana DeLouis


On Aug 26, 6:48 am, "Dr. Wolfgang Hintze" <w... at snafu.de> wrote:
> Given the list
>
> a={1,1,2,1,2};
>
> we can reduce multiple instances of elements using
>
> b=Union[a]
> {1,2}
>
> The question is now how to compute the list of multiple elements.
> In our example this would be m={1,1,2}.
>
> A possible solution is
>
> m[x_]:= Flatten[Take[#, {1, Length[#] - 1}] & /@ Select[Split[x],
> Length[#] > 1 &]]
>
> m[a]
> {1,1,2}
>
> I'm sure there is a much more elegant solution. Can you suggest one?


  • Prev by Date: Re: Getting started with 3D cardioid
  • Next by Date: Re: Plot of (2 x^2 - x^3)^(1/3)
  • Previous by thread: Re: List of multiple elements
  • Next by thread: Re: List of multiple elements