To check if a specific element is present in a list in Kotlin, you can use the contains
method of the List class.
In this example,
myList
with some elements.contains
method to check if the specified element is present in the list.contains
method returns true
if the element is present, otherwise false
.fun main() {
val myList = listOf(10, 20, 30, 40, 50)
val searchElement = 40
val isPresent = myList.contains(searchElement)
println("Element presence in list: $isPresent")
}
Element presence in list: true
In this example,
myList
with some string elements.contains
method to check if the specified string is present in the list.contains
method returns true
if the string is present, otherwise false
.fun main() {
val myList = listOf("apple", "banana", "cherry", "date", "elderberry")
val searchElement = "cherry"
val isPresent = myList.contains(searchElement)
println("Element presence in list: $isPresent")
}
Element presence in list: true
In this tutorial, we learned How to check if a Specific Element is present in the List in Kotlin language with well detailed examples.