Using the 'count' Function for Counting Elements in Collections

The 'count' function in Kotlin allows you to quickly determine the number of elements in a collection that meet a specific condition. This is particularly useful when you want to filter elements based on a predicate without manually iterating through the collection.

val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val evenCount = numbers.count { it % 2 == 0 }
println("Number of even numbers: $evenCount") // Output: Number of even numbers: 5