count
count
functions returns either the total number of elements in a collection or the number of elements matching the given predicate.
fun main() {
val numbers = listOf(1, -2, 3, -4, 5, -6) // 1
val totalCount = numbers.count() // 2
val evenCount = numbers.count { it % 2 == 0 } // 3
println("Total number of elements: $totalCount")
println("Number of even elements: $evenCount")
}
- Defines a collection of numbers.
- Counts the total number of elements.
- Counts the number of even elements.