max(count:)

Returns the largest elements of this sequence.

MinMax.swift:216
func max(count: Int) -> [Element]

Parameters

count

The number of elements to return. If count is greater than the number of elements in this sequence, all of the sequence’s elements are returned.

Returns

An array of the largest count elements of this sequence.

This example partially sorts an array of integers to retrieve its three largest values:

let numbers = [7, 1, 6, 2, 8, 3, 9]
let largestThree = numbers.max(count: 3)
// [7, 8, 9]

If you need to sort a sequence but only need to access its largest elements, using this method can give you a performance boost over sorting the entire sequence. The order of equal elements is guaranteed to be preserved.