Kotlin Array count Function

The count function in Kotlin is used to count the number of elements in an array that satisfy a given predicate. This function is part of the Kotlin standard library and provides a convenient way to count elements based on a condition.

Table of Contents

  1. Introduction
  2. count Function Syntax
  3. Understanding count
  4. Examples
    • Basic Usage
    • Counting Elements with a Condition
    • Using count with Custom Types
  5. Real-World Use Case
  6. Conclusion

Introduction

The count function returns the number of elements in the array that match a specified condition. This is useful for filtering and counting elements based on specific criteria.

count Function Syntax

The syntax for the count function is as follows:

fun <T> Array<out T>.count(predicate: (T) -> Boolean): Int

Parameters:

  • predicate: A lambda function that takes an element of type T and returns true if the element matches the condition, false otherwise.

Returns:

  • The number of elements that satisfy the predicate.

Understanding count

The count function iterates through the array and applies the given predicate to each element. It counts and returns the number of elements for which the predicate returns true.

Examples

Basic Usage

To demonstrate the basic usage of count, we will create an array of integers and count the number of elements.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val totalCount = numbers.count()
    println("Total number of elements: $totalCount")
}

Output:

Total number of elements: 5

Counting Elements with a Condition

This example shows how to use count to count the number of elements that satisfy a specific condition.

Example

fun main() {
    val numbers = arrayOf(1, 2, 3, 4, 5)
    val evenCount = numbers.count { it % 2 == 0 }
    println("Number of even elements: $evenCount")
}

Output:

Number of even elements: 2

Using count with Custom Types

This example demonstrates how to use count with an array of custom objects.

Example

data class Person(val name: String, val age: Int)

fun main() {
    val people = arrayOf(
        Person("Ravi", 25),
        Person("Anjali", 30),
        Person("Priya", 22)
    )

    val adultsCount = people.count { it.age >= 25 }
    println("Number of adults: $adultsCount")
}

Output:

Number of adults: 2

Real-World Use Case

Counting Elements Based on a Condition

In real-world applications, the count function can be used to count elements based on specific conditions, such as counting the number of valid data points, active users, or completed tasks.

Example

data class Task(val title: String, val isCompleted: Boolean)

fun main() {
    val tasks = arrayOf(
        Task("Task 1", true),
        Task("Task 2", false),
        Task("Task 3", true),
        Task("Task 4", false)
    )

    val completedTasksCount = tasks.count { it.isCompleted }
    println("Number of completed tasks: $completedTasksCount")
}

Output:

Number of completed tasks: 2

Conclusion

The count function in Kotlin is a convenient method for counting the number of elements in an array that satisfy a given predicate. It provides a simple way to filter and count elements based on specific criteria. By understanding and using this function, you can effectively manage counting operations in your Kotlin applications.

Comments