- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - contains() Function
The Kotlin array contains() function checks if the specified element is present in the array. It returns true if the element is found; otherwise, it returns false.
This function is useful for searching for a specific value in the array.
Syntax
Following is the syntax of Kotlin array contains() function −
operator fun <T> Array<out T>.contains(element: T): Boolean
Parameters
This function accepts an element as a parameter that represents an element of an array to be searched.
Return value
This function returns a boolean value: true if the array contains the element, and false otherwise.
Example 1
Following is the basic example, we create an array displaying the elements. We then use contains() function to check specified element is present or not −
fun main(args: Array<String>) {
var array = Array(10) { i -> i}
var result = array.count()
println("The elements in the array is(are): ")
for (i in 0..result-1) {
println("array[$i] = ${array[i]}")
}
val check = 100
if(array.contains(check)) {
println("The array contains the element $check")
} else {
println("The array doesn't contain the element $check")
}
}
Output
Following is the output −
The elements in the array is(are): array[0] = 0 array[1] = 1 array[2] = 2 array[3] = 3 array[4] = 4 array[5] = 5 array[6] = 6 array[7] = 7 array[8] = 8 array[9] = 9 The array doesn't contain the element 100
Example 2
Now, let's see another example. Here, we create an array. We then use contains() to check whether specified element is present −
fun main(args: Array<String>) {
var array = Array(5) { i -> i}
var result = array.count()
val check = 4
if(array.contains(check)) {
println("The array contains $check")
} else {
println("The array doesn't contain the element $check")
}
}
Output
Following is the output −
The array contains 4
Example 3
The example below creates an array with random values. Then, we use the contains() to check the specified element is available or not −
fun main(args: Array<String>) {
var array = arrayOf(10, 23.4, "kotlin", "tutorialspoint")
var result = array.count()
println("The elements in the array are: ")
for (i in 0..result-1) {
println("array[$i] = ${array[i]}")
}
val check = "tutorix"
if(array.contains(check)) {
println("The array contains the element $check")
} else {
println("The array doesn't contain the element $check")
}
}
Output
Following is the output −
The elements in the array are: array[0] = 10 array[1] = 23.4 array[2] = kotlin array[3] = tutorialspoint The array doesn't contain the element tutorix