F# Friday – The Seq.distinct Function

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 thought on “F# Friday – The Seq.distinct Function”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.