Delete repeating list elements preserving order of appearance

后端 未结 3 550
我在风中等你
我在风中等你 2020-12-03 06:14

I am producing flat lists with 10^6 to 10^7 Real numbers, and some of them are repeating.

I need to delete the repeating instances, keeping the first occurrence o

3条回答
  •  旧时难觅i
    2020-12-03 06:43

    For versions of Mathematica before 7, 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.

    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]]]]
    

提交回复
热议问题