Re: List of multiple elements
- To: mathgroup at smc.vnet.net
- Subject: [mg112139] Re: List of multiple elements
- From: Raffy <adraffy at gmail.com>
- Date: Wed, 1 Sep 2010 06:29:13 -0400 (EDT)
- References: <i5d019$bs8$1@smc.vnet.net> <i5g0hu$gos$1@smc.vnet.net> <i5idoj$jr6$1@smc.vnet.net>
On Aug 31, 1:16 am, Ray Koopman <koop... at sfu.ca> wrote: > On Aug 30, 3:18 am, Ray Koopman <koop... at sfu.ca> wrote: > > > > > dupels2 is a faster version of DuplicateElements: > > > dupels2[v_List] := Module[{f,dups={}}, > > f[n_] := (f[n] := (dups = {dups,n})); f/@v; Flatten@dups] > > dupels1 is twice as fast as dupels2. > > dupels1[v_List] := Module[{f}, f[x_] := ((f[x] = x); {}); > Flatten[f= /@v]] > > {m,n} = {50,1*^4} > Timing[w = Table[Random[Integer,{1,m}],{n}]; Do[Null,{1*^7}]]; > First@AbsoluteTiming[t1 = dupels1[w];] > First@AbsoluteTiming[t2 = dupels2[w];] > t1 === t2 > > {50,10000} > 0.030357 Second > 0.060681 Second > True I'm pretty sure the solution I posted above is the fastest unless more assumptions are made. Most of the solutions listed require multiple searches over the elements or require sorting neither of which are necessary. dup3[v_] := Join @@ (ConstantArray[#1, #2 - 1] & @@@ Tally[v]); v = RandomInteger[{1, 100}, 10000000]; dup3[v]; // Timing ==> 0.031 sordups[v]; // Timing ==> 2.87