Unsorted Union

Author

Eric W. Weisstein
May 5, 2007

This notebook downloaded from http://mathworld.wolfram.com/notebooks/SetTheory/UnsortedUnion.nb.

For more information, see Eric's MathWorld entry http://mathworld.wolfram.com/UnsortedUnion.html.

©2007 Wolfram Research, Inc. except for portions noted otherwise

Code

UnsortedUnion1[x_]:=First/@Tally[x]
UnsortedUnion2[x_]:=Module[{f},f[y_]:=(f[y]=Sequence[];y);f/@x]
UnsortedUnion3[x_]:=Reap[Sow[1,x],_,#1&][[2]]

Timings

l=RandomInteger[10^3,5 10^5];

Timing[#[l]][[1]]&/@{UnsortedUnion1,UnsortedUnion2,UnsortedUnion3}

l2=Flatten[First/@FactorInteger[#^2+1]&/@Range[5 10^4]];

Timing[#[l2]][[1]]&/@{UnsortedUnion1,UnsortedUnion2,UnsortedUnion3}

l3=Range[10^6];

Timing[#[l3]][[1]]&/@{UnsortedUnion1,UnsortedUnion2,UnsortedUnion3}