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

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

liboz|Last Updated: 8/18/2017

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

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

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

Syntax

// Signature:
Array.distinct : 'T [] -> 'T [] (requires equality)

// Usage:
Array.distinct source

Parameters

source Type: 'T []

The input array.

Exceptions

ExceptionCondition
ArgumentNullExceptionThrown when the input array is null

Return Value

The result array.

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 Array.distinct. The example generates the binary representation of a number as a array. 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 |> Array.ofList

printfn "%A" (binary 1024)

let resultArray = Array.distinct (binary 1024)
printfn "%A" resultArray

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.Array Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2018 Microsoft