TOPICS
Search

Unsorted Union


The unsorted union of a list S is a list containing the same elements as S but with the second and subsequent occurrence of any given element removed. For example, the unsorted union of the set {1,3,3,2,1,3,4} is {1,3,2,4}. The unsorted union differs from the usual union {1,2,3,4} in that the elements of the unsorted union are not necessarily ordered.

The unsorted union is implemented in the Wolfram Language as DeleteDuplicates[list].

It can be implemented in the Wolfram Language top-level code as:

  UnsortedUnion1[x_] := Tally[x][[All, 1]]

or

  UnsortedUnion2[x_] := Reap[Sow[1, x], _, #1&][[2]]

or

  UnsortedUnion3[x_] := Module[{f},
    f[y_] := (f[y] = Sequence[]; y);
    f /@ x
  ]

Depending on the nature of the list to be unioned, different implementations above may be more efficient, although in general, the first is the fastest.


See also

Disjoint Union, Union

Explore with Wolfram|Alpha

Cite this as:

Weisstein, Eric W. "Unsorted Union." From MathWorld--A Wolfram Web Resource. https://mathworld.wolfram.com/UnsortedUnion.html

Subject classifications