This article explores different ways to count items in a list matching a predicate in Kotlin.

1. Using size property

The standard solution to find the number of elements in a list in Kotlin is calling its size property.

Download Code

 
To count only items that match a predicate, use the filter() method:

Download Code

2. Using count() function

Another possibility is using the count() function, which returns the count of elements in the list.

Download Code

 
To count the number of elements matching a predicate, you can do like below:

Download Code

That’s all about counting items in a list matching a predicate in Kotlin.