Jump to content

Greatest element of a list: Difference between revisions

Line 2,762:
 
=={{header|Kotlin}}==
Kotlin already has a 'max'() function in its standard library sothat weapplies useto collection of Iterable that:
<syntaxhighlight lang="scala">// version 1.0.5-2
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()}")
}</syntaxhighlight>
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
 
<syntaxhighlight lang="scalakotlin">// version 1.0.5-2
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
}
}</syntaxhighlight>
 
=={{header|Lambdatalk}}==
19

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.