Array – MAX and MIN in Float arrays in SWIFT

According to this answer, to get the maximum value of the array, we can do:

let nums = [1, 6, 3, 9, 4, 6];
let numMax = nums.reduce(Int.min, {max($0, $1) })

How do we do for Array< Float> The same thing, because Float does not have min and max?

let floats: Array = [2.45, 7.21, 1.35, 10.22, 2.45, 3];

The solution given here https://stackoverflow.com/a/24161004/1187415 is applicable
for the sequence of all comparable elements, so for A series of floating point numbers:

let floats: Array = [2.45, 7.21, 1.35, 10.22, 2.45, 3]
let numMax = maxElement(floats)

maxElement() is defined in the Swift library as

/// Returns the maximum element in `elements`. Requires:
/// `elements` is non-empty. O(countElements(elements))
func maxElement(elements: R) -> R.Generator. Element

According to this answer, to get the maximum value of the array, we can do:

let nums = [1, 6, 3, 9, 4, 6];
let numMax = nums.reduce(Int.min, {max($0, $1) })

How do we Array does the same thing, because Float does not have min and max?

let floats: Array = [2.45, 7.21, 1.35, 10.22, 2.45, 3];

The solution given here https://stackoverflow.com/a/24161004/1187415 is applicable
for a sequence of all comparable elements, so for a series of floating point numbers:

let floats: Array = [2.45, 7.21, 1.35, 10.22, 2.45, 3]
let numMax = maxElement(floats)

maxElement () is defined in the Swift library as

/// Returns the maximum element in `elements`. Requires:
/// `elements` is non-empty . O(countElements(elements))
func maxElement(elements: R) -> R.Generator.Element

Leave a Comment

Your email address will not be published.