Kotlin Program to Check if An Array Contains a Given Value

Checking if an array contains a specific value is a common operation in programming. Kotlin provides several ways to accomplish this task efficiently. This article will explore three different Kotlin Program to Check if An Array Contains a Given Value, including detailed explanations, code implementations, and outputs for each example.

1. Using the in Operator

1.1 Program Explanation

The in operator is a concise and readable way to check if an array contains a specific value. This operator returns true if the value is present in the array and false otherwise.

1.2 Code Implementation

Kotlin
fun main() {
    val array = arrayOf("Apple", "Banana", "Cherry")
    val valueToCheck = "Banana"

    // Check if the array contains the value using 'in' operator
    val containsValue = valueToCheck in array

    println("Array: ${array.joinToString()}")
    println("Does the array contain '$valueToCheck'? $containsValue")
}

1.3 Output

Kotlin
Array: Apple, Banana, Cherry
Does the array contain 'Banana'? true

2. Using the contains Method

2.1 Program Explanation

The contains method is another straightforward way to check if an array contains a given value. This method is called on the array and takes the value to be checked as a parameter.

2.2 Code Implementation

Kotlin
fun main() {
    val array = arrayOf(1, 2, 3, 4, 5)
    val valueToCheck = 3

    // Check if the array contains the value using 'contains' method
    val containsValue = array.contains(valueToCheck)

    println("Array: ${array.joinToString()}")
    println("Does the array contain '$valueToCheck'? $containsValue")
}

2.3 Output

Kotlin
Array: 1, 2, 3, 4, 5
Does the array contain '3'? true

3. Using the any Function

3.1 Program Explanation

The any function is a more functional approach to check if an array contains a specific value. This function takes a predicate and returns true if at least one element matches the predicate.

3.2 Code Implementation

Kotlin
fun main() {
    val array = arrayOf("Kotlin", "Java", "Python")
    val valueToCheck = "Swift"

    // Check if the array contains the value using 'any' function
    val containsValue = array.any { it == valueToCheck }

    println("Array: ${array.joinToString()}")
    println("Does the array contain '$valueToCheck'? $containsValue")
}

3.3 Output

Kotlin
Array: Kotlin, Java, Python
Does the array contain 'Swift'? false

Conclusion

In this article, we explored three different methods to check if an array contains a given value in Kotlin. The first method used the in operator, which provides a concise and readable way to perform this check. The second method employed the contains method, a straightforward and commonly used approach. The third method utilized the any function, offering a functional programming style to check for the presence of a value.

Each method has its advantages and can be used based on the specific requirements of your program. Understanding these techniques allows you to effectively manage and manipulate arrays in your Kotlin applications.