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

**URL:** <https://discuss.kotlinlang.org/t/is-there-a-way-to-find-the-max-value-of-a-1d-array-within-one-line-of-code/21536>\
**Category:** Support\
**Created:** [April 14, 2021, 6:48pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-find-the-max-value-of-a-1d-array-within-one-line-of-code/21536 "2021-04-14T18:48:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonuser](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@anonuser](https://discuss.kotlinlang.org/u/anonuser)\
**Post date:** [April 14, 2021, 6:48pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-find-the-max-value-of-a-1d-array-within-one-line-of-code/21536/1 "2021-04-14T18:48:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [April 14, 2021, 7:03pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-find-the-max-value-of-a-1d-array-within-one-line-of-code/21536/2 "2021-04-14T19:03:58Z")

</div>

It should be working correctly. Try editing this example:

```run-kotlin
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())
}

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [April 14, 2021, 7:06pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-find-the-max-value-of-a-1d-array-within-one-line-of-code/21536/3 "2021-04-14T19:06:31Z")

</div>

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

```run-kotlin
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)
}

```

---

<div class="post-metadata">

**Author:** ![anonuser](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@anonuser](https://discuss.kotlinlang.org/u/anonuser)\
**Post date:** [April 14, 2021, 7:11pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-find-the-max-value-of-a-1d-array-within-one-line-of-code/21536/4 "2021-04-14T19:11:29Z")

</div>

it ended up working, thank you
