Table of contents
TOC
Collapse the table of content
Expand the table of content

List.distinct<'T> Function (F#)

liboz|Last Updated: 8/18/2017

Returns a list that contains no duplicate entries according to generic hash and equality comparisons on the entries. If an element occurs multiple times in the list then the later occurrences are discarded.

Namespace/Module Path: Microsoft.FSharp.Collections.List

Assembly: FSharp.Core (in FSharp.Core.dll)

Syntax

// Signature:
List.distinct : 'T list -> 'T list (requires equality)

// Usage:
List.distinct source

Parameters

source Type: 'Tlist

The input list.

Exceptions

ExceptionCondition
ArgumentNullExceptionThrown when the input list is null

Return Value

The result list.

Remarks

This function is named Distinct in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.

Example

The following example demonstrates the use of List.distinct. The example generates the binary representation of a number as a list. It then determines that the only unique numbers are 0 and 1.

let binary n =
    let rec generateBinary n =
        if (n / 2 = 0) then [n]
        else (n % 2) :: generateBinary (n / 2)
    generateBinary n |> List.rev

printfn "%A" (binary 1024)

let resultList = List.distinct (binary 1024)
printfn "%A" resultList

Output

[1; 0; 0; 0; 0; 0; 0; 0; 0; 0; 0]
[1; 0]

Platforms

Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 4.0, Portable

See Also

Collections.List Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2018 Microsoft