MathGroup Archive 2005

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

Search the Archive

Re: Re: Unsorted Union

  • To: mathgroup at smc.vnet.net
  • Subject: [mg61639] Re: [mg61564] Re: Unsorted Union
  • From: Adriano Pascoletti <pascolet at dimi.uniud.it>
  • Date: Mon, 24 Oct 2005 21:06:57 -0400 (EDT)
  • References: <dj4p6t$gpt$1@smc.vnet.net><dj721s$cpb$1@smc.vnet.net> <200510220724.DAA12393@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Il giorno 22 ott 2005, alle ore 09:24, Bill White ha scritto:

> Other interesting things can be done with this memoizing scheme.  For
> example, it can be used to collect the duplicate elements in a list:
>
> In[11]:=
> DuplicateElements[list_List]:=
>   Module[{f,dups={}},
>     f[n_]:=(f[n]:=AppendTo[dups,n]);
>     f/@list;
>     dups];
>
> In[12]:=
> list=Table[Random[Integer,{1,10}],{10}]
>
> Out[12]=
> {3,10,7,10,2,2,6,9,6,9}
>
> In[13]:=
> DuplicateElements[list]
>
> Out[13]=
> {10,2,6,9}
>
> Cheers -
>
> bw
>
>

A very efficient approach to duplicates is based on Sow and Reap  
(Mathematica v. 5):

dups = Reap[Sow[1, #], _, If[Length[#2] > 1, #1, Sequence @@ {}] &] 
[[2]] &;

L=Table[Random[Integer,{-1000,1000}],{10000}];
Timing[DuplicateElements[L]][[1]]
Timing[dups[L]][[1]]

3.95812 Second

0.105166 Second

L=Table[Random[Integer,{-5000,5000}],{20000}];Timing[DuplicateElements 
[L]][[1]]
Timing[dups[L]][[1]]

21.3677 Second

0.596305 Second

If you want the multeplicities of repeated elements

dups1 = Reap[
     Sow[1, #], _, If[Length[#2] > 1, {#1, Length@#2}, Sequence @@  
{}] &][[2]] &;

list = Table[Random[Integer, {1, 10}], {10}]

{5, 4, 6, 9, 9, 9, 3, 6, 10, 4}

dups1[list]

{{4,2},{6,2},{9,3}}

L = Table[Random[Integer, {-5000, 5000}], {20000}];
Timing[DuplicateElements[L]][[1]]
Timing[dups1[L]][[1]]

Out[40]=
30.258 Second

Out[41]=
0.557876 Second


Adriano Pascoletti


  • Prev by Date: Re: Warning from Piecewise
  • Next by Date: Re: Zero-based indexing option?
  • Previous by thread: Re: Re: Unsorted Union
  • Next by thread: Display sound but suppress graphics