Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structureDeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerationsperformance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsetsSelecting minimal subsets

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]
added 223 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]

In version 7 or later, use the DeleteDuplicates function. (See also DeleteDuplicatesBy, introduced in version 10, but be aware of performance considerations.)

For versions of Mathematica before 7, when DeleteDuplicates was introduced, and for general interest, here are several ways of implementing the UnsortedUnion (i.e. DeleteDuplicates) function. These are collected from the help docs and MathGroup. They have been adjusted to accept multiple lists which are then joined, in analogy to Union. Unlike Union, these functions do not sort the list in the process of removing duplicates.

These methods may be obsolete for the specific function of DeleteDuplicates but they demonstrate methods that continue to be useful in more general problems.

  1. DeleteDuplicates while retaining sublist structure
  2. Selecting minimal subsets
  • The Sow/Reap method demonstrates sowing one object to multiple tags, the reverse of its most common use, to powerful effect.

  • The Tally method can be generalized to GatherBy.

###For Mathematica 4 or earlier [ref]

UnsortedUnion = Module[{f}, f[y_] := (f[y] = Sequence[]; y); f /@ Join@##] &

###For Mathematica 5 [ref]

UnsortedUnion[x__List] := Reap[Sow[1, Join@x], _, # &][[2]]

###For Mathematica 6

UnsortedUnion[x__List] := Tally[Join@x][[All, 1]]

###From Leonid Shifrin for Mathematica 3+

unsortedUnion[x_List] := 
  Extract[x, Sort[Union[x] /. Dispatch[MapIndexed[Rule, x]]]]
added 235 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 224 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
I am trying to make this answer stand on its own and not just be complementary to the other ones.
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k
Loading
added 97 characters in body
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k
Loading
added 4 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
added 64 characters in body
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading
Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k
Loading