Averages/Mode: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: update) |
(Added Kotlin) |
||
Line 1,424: | Line 1,424: | ||
3 4</lang> |
3 4</lang> |
||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.0.6 |
|||
fun <T> modeOf(a: Array<T>) { |
|||
val sortedByFreq = a.groupBy { it }.entries.sortedByDescending { it.value.size } |
|||
val maxFreq = sortedByFreq.first().value.size |
|||
val modes = sortedByFreq.takeWhile { it.value.size == maxFreq } |
|||
if (modes.size == 1) |
|||
println("The mode of the collection is ${modes.first().key} which has a frequency of $maxFreq") |
|||
else { |
|||
print("There are ${modes.size} modes with a frequency of $maxFreq, namely : ") |
|||
println(modes.map { it.key }.joinToString(", ")) |
|||
} |
|||
} |
|||
fun main(args: Array<String>) { |
|||
val a = arrayOf(7, 1, 1, 6, 2, 4, 2, 4, 2, 1, 5) |
|||
println("[" + a.joinToString(", ") + "]") |
|||
modeOf(a) |
|||
println() |
|||
val b = arrayOf(true, false, true, false, true, true) |
|||
println("[" + b.joinToString(", ") + "]") |
|||
modeOf(b) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
[7, 1, 1, 6, 2, 4, 2, 4, 2, 1, 5] |
|||
There are 2 modes with a frequency of 3, namely : 1, 2 |
|||
[true, false, true, false, true, true] |
|||
The mode of the collection is true which has a frequency of 4 |
|||
</pre> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |