F#: removing duplicates from a seq is slow

后端 未结 9 1182
半阙折子戏
半阙折子戏 2021-01-12 03:11

I am attempting to write a function that weeds out consecutive duplicates, as determined by a given equality function, from a seq<\'a> but with a twist:

9条回答
  •  时光取名叫无心
    2021-01-12 04:08

    I'm also looking forward to a non-seq answer. Here's another solution:

    let t = [("a", 1); ("b", 2); ("b", 3); ("b", 4); ("c", 5)]
    t |> Seq.groupBy fst |> Seq.map (snd >>  Seq.last)
    

    I tested on a 1M list:

    Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
    val it : seq = seq [(2, 2); (1, 1)]
    

提交回复
热议问题