Re: List of multiple elements

*To*: mathgroup at smc.vnet.net*Subject*: [mg112144] Re: List of multiple elements*From*: Ray Koopman <koopman at sfu.ca>*Date*: Thu, 2 Sep 2010 02:30:28 -0400 (EDT)*References*: <i5d019$bs8$1@smc.vnet.net> <i5g0hu$gos$1@smc.vnet.net>

On Sep 1, 3:29 am, Raffy <adra... at gmail.com> wrote: > 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 I agree that if speed matters then any code using Split at Sort[...] should be reconsidered. In the present case the determiners seem to be the length of the list, the number of different values in it, and the user's requirements regarding the ordering of the results. If ordering does not matter then dup3 will be faster for longer lists with fewer different values, and sordups will be faster for shorter lists with more different values. For lists with less than 10^4 elements, the critical ratio of number of different values to total list length seems to be in the neighborhood of 10%. For lists with more than 10^4 elements, sordups slows suddenly, suggesting to me that there may be a change in the kernel implementation for longer lists.