minAndMax
Returns both the minimum and maximum elements in the sequence.
func minAndMax() -> (min: Element, max: Element)?
Returns
A tuple with the sequence’s minimum element, followed by its maximum element. If the sequence provides multiple qualifying minimum elements, the first equivalent element is returned; of multiple maximum elements, the last is returned. If the sequence has no elements, the method returns nil
.
This example finds the smallest and largest values in an array of height measurements.
let heights = [67.5, 65.7, 64.3, 61.1, 58.5, 60.3, 64.9]
if let (lowestHeight, greatestHeight) = heights.minAndMax() {
print(lowestHeight, greatestHeight)
} else {
print("The list of heights is empty")
}
// Prints: "58.5 67.5"