Greatest element of a list: Difference between revisions
Content added Content deleted
Line 2,762: | Line 2,762: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Kotlin |
Kotlin has a max() function in its standard library that applies to collection of Iterable : |
||
⚫ | |||
fun main(args: Array<String>) { |
|||
print("Number of values to be input = ") |
|||
val n = readLine()!!.toInt() |
|||
val array = DoubleArray(n) |
|||
for (i in 0 until n) { |
|||
print("Value ${i + 1} = ") |
|||
array[i] = readLine()!!.toDouble() |
|||
} |
|||
println("\nThe greatest element is ${array.max()}") |
|||
⚫ | |||
Example of use: |
|||
{{out}} |
|||
<pre> |
|||
Number of values to be input = 4 |
|||
Value 1 = 70.5 |
|||
Value 2 = 23.67 |
|||
Value 3 = 150.2 |
|||
Value 4 = 145 |
|||
⚫ | |||
The greatest element is 150.2 |
|||
fun main() { |
|||
</pre> |
|||
listOf(1.0, 3.5, -1.1).max().also { println(it) } // 3.5 |
|||
listOf(1, 3, -1).max().also { println(it) } // 3 |
|||
setOf(1, 3, -1).max().also { println(it) } // 3 |
|||
} |
|||
⚫ | |||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |