Averages/Mode: Difference between revisions

Content added Content deleted
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 1,425: Line 1,425:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<lang scala>fun <T> modeOf(a: Array<T>) {
fun <T> modeOf(a: Array<T>) {
val sortedByFreq = a.groupBy { it }.entries.sortedByDescending { it.value.size }
val sortedByFreq = a.groupBy { it }.entries.sortedByDescending { it.value.size }
val maxFreq = sortedByFreq.first().value.size
val maxFreq = sortedByFreq.first().value.size
Line 1,434: Line 1,433:
else {
else {
print("There are ${modes.size} modes with a frequency of $maxFreq, namely : ")
print("There are ${modes.size} modes with a frequency of $maxFreq, namely : ")
println(modes.map { it.key }.joinToString(", "))
println(modes.map { it.key }.joinToString(", "))
}
}
}
}
Line 1,445: Line 1,444:
val b = arrayOf(true, false, true, false, true, true)
val b = arrayOf(true, false, true, false, true, true)
println("[" + b.joinToString(", ") + "]")
println("[" + b.joinToString(", ") + "]")
modeOf(b)
modeOf(b)
}</lang>
}</lang>