Today’s F# Friday is a simple one: Seq.distinct
. Seq.distinct
removes any duplicate members of a sequence, leaving only unique values.
One way to remove duplicates is to turn your sequence into a set with Set.ofSeq
:
let noDupes = seq [3;5;6;3;3;7;1;1;7;3;2;7] |> Set.ofSeq // val noDupes : Set<int> = // set [1; 2; 3; 5; 6; 7]
That’s fine if you don’t care about preserving the order of the items in the input sequence.
But if you do want to preserve the order, Seq.distinct
is the ticket:
let noDupesOrdered = seq [3;5;6;3;3;7;1;1;7;3;2;7] |> Seq.distinct // val noDupesOrdered : seq<int> = // seq [3; 5; 6; 7; 1; 2]
One reply on “F# Friday – The Seq.distinct Function”
[…] F# Friday – The Seq.distinct Function – Brad Collins […]