Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get Maximum value of an Array

    let array = [1,5, 10, 2, 20, 9, 3]
let maximumNumber = array.reduce(Int.min, { max($0, $1)})
print(maximumNumber)

This is my code but I need the Maximum value of an Array without any predefined functions and HigherOrder Functions.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    func findMaxNumber(inArray : [Int]) -> Int?{
    var max = inArray.first
    if inArray.isEmpty == false{
        for i in 0..<inArray.count{
            let x = inArray[i]
            if x > max! {
                max = x
            }
        }
    }
    return max
}

Usage let maxNumber = findMaxNumber(inArray: yourIntArray)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading