Kotlin Program to Find Largest Element in an Array

Kotlin Program to Find Largest Element in an Array. Note that it is the kth largest element in the sorted order, not the kth distinct element. In the below program, we store the first element of the array in the variable largest.Then, the largest is used to compare other elements in the array. If any number is greater than the largest, the largest is assigned the number.

Kotlin Program to Find Largest Element in an Array

Source Code

fun main(args: Array<String>) {
    val numArray = doubleArrayOf(23.4, -34.5, 50.0, 33.5, 55.5, 43.7, 5.7, -66.5)
    var largest = numArray[0]

    for (num in numArray) {
        if (largest < num)
            largest = num
    }

    println("Largest element = %.2f".format(largest))
}

Output