MathGroup Archive 2010

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

Search the Archive

Re: List of multiple elements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg112088] Re: List of multiple elements
  • From: Ray Koopman <koopman at sfu.ca>
  • Date: Mon, 30 Aug 2010 06:18:33 -0400 (EDT)
  • References: <i5d019$bs8$1@smc.vnet.net>

On Aug 28, 11:51 pm, Dana DeLouis <dana.... at gmail.com> wrote:
>> 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 DuplicateElements 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

dupels2 is a faster version of DuplicateElements:

dupels2[v_List] := Module[{f,dups={}},
f[n_] := (f[n] := (dups = {dups,n})); f/@v; Flatten@dups]

DupsOnlee is an unsorted version of DupsOnly:

DupsOnlee[v_List] := Fold[DeleteCases[#1, #2, {1}, 1] &, v, Union@v]

DupsOnlee slows as Length@Union@v/Length@v increases;
dupels2 does not. The crossover seems to be at about 5/1000.

(The dummy call to Timing is necessary to avoid the problem noted in
http://groups.google.ca/group/comp.soft-sys.math.mathematica/msg/1125a9dbeae0d5db
)

{m,n} = {40,1*^4}
Timing[w = Table[Random[Integer,{1,m}],{n}]; Do[Null,{1*^7}]];
First@AbsoluteTiming[t1 = dupels2[w];]
First@AbsoluteTiming[t2 = DupsOnlee[w];]
t1 === t2

{40,10000}
0.068693 Second
0.059314 Second
True

{m,n} = {50,1*^4}
Timing[w = Table[Random[Integer,{1,m}],{n}]; Do[Null,{1*^7}]];
First@AbsoluteTiming[t1 = dupels2[w];]
First@AbsoluteTiming[t2 = DupsOnlee[w];]
t1 === t2

{50,10000}
0.069162 Second
0.072681 Second
True

{m,n} = {60,1*^4}
Timing[w = Table[Random[Integer,{1,m}],{n}]; Do[Null,{1*^7}]];
First@AbsoluteTiming[t1 = dupels2[w];]
First@AbsoluteTiming[t2 = DupsOnlee[w];]
t1 === t2

{60,10000}
0.066558 Second
0.100195 Second
True

>
> 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: Plot of (2 x^2 - x^3)^(1/3)
  • Next by Date: Re: i get an empty figure when exporting it, if I keep Epilogue->{.....}
  • Previous by thread: Re: List of multiple elements
  • Next by thread: Re: List of multiple elements