0

How to solve it? I should get 2 in this case.

var r = [1, 3, 5]
r.max() // gives 5

ps: new quality standarts when posting - is something crazy...

3

1 Answer 1

3

This is how I would go about doing it:

let array = [1, 3, 5]

if let (maxIndex, maxValue) = array.enumerated().max(by: { $0.element < $1.element }) {
    print("The max element is \(maxValue) at index \(maxIndex)")
}
else {
    print("The array is empty, and has no max element or index.")
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.