To check if a specified element is present in an array in Kotlin, you can use the contains
method of the Array class.
In this example,
arr
with integer values.contains
method of the array object arr
to check if a specific integer is present in the array.contains
method returns true
if the element is found in the array, and false
otherwise.fun main(args: Array<String>) {
val arr = arrayOf(1, 2, 3, 4, 5)
val isPresent = arr.contains(3)
println("Is element 3 present: $isPresent")
}
Is element 3 present: true
In this example,
arr
with integer values.contains
method of the array object arr
to check if a specific integer is present in the array.contains
method returns true
if the element is found in the array, and false
otherwise.fun main(args: Array<String>) {
val arr = arrayOf(1, 2, 3, 4, 5)
val isPresent = arr.contains(6)
println("Is element 6 present: $isPresent")
}
Is element 6 present: false
In this example,
arr
with string values.contains
method of the array object arr
to check if a specific string is present in the array.contains
method returns true
if the element is found in the array, and false
otherwise.fun main(args: Array<String>) {
val arr = arrayOf("apple", "banana", "cherry", "date")
val isPresent = arr.contains("cherry")
println("Is element 'cherry' present: $isPresent")
}
Is element 'cherry' present: true
In this example,
arr
with string values.contains
method of the array object arr
to check if a specific string is present in the array.contains
method returns true
if the element is found in the array, and false
otherwise.fun main(args: Array<String>) {
val arr = arrayOf("apple", "banana", "cherry", "date")
val isPresent = arr.contains("fig")
println("Is element 'fig' present: $isPresent")
}
Is element 'fig' present: false
In this tutorial, we learned How to check if Specified Element is present in the Array in Kotlin language with well detailed examples.