Is there a way to find the max value of a 1D array within one line of code?

As titled.

I’m working on a problem that requires me to find the max value of a Double 1D Array and have tried max() but doesn’t work. Hoping someone would know how to do it

1 Like

It should be working correctly. Try editing this example:

fun main() {
    println(arrayOf(1.0, 1.5, 2.5, 2.0, 8.0, 2.0, 1.0, 0.0, -5.0, 78.0).max())
}
1 Like

If you really wanna do it yourself though, this should work:

fun main() {
    val dobArray = arrayOf(1.0, 1.5, 2.5, 2.0, 8.0, 2.0, 1.0, 0.0, -5.0, 78.0)
    var maximumValue: Double = Double.MIN_VALUE
    for(i in dobArray){
        maximumValue = if(i > maximumValue) i else maximumValue
    }
    println(maximumValue)
}
2 Likes

it ended up working, thank you

1 Like