The contains()
method is used to check if an element occurs in a list.
This method takes one parameter and returns a boolean value indicating whether or not that item is found in the list.
list_name.contains(searchItem);
list_name
is the name of the list to be searched.
The searchItem
represents the item to be found.
The method contains()
returns true
if the item is found, otherwise it returns false
.
The following example shows how to use the contains()
method. Click on the “Run” button to execute the code.
void main(){// Creating listvar myList = [20, 15, 37, 23, 60, 50, 20];// Item to be foundint searchItem = 23;// stores boolean in variable foundbool found = myList.contains(searchItem);if (found) {print("$searchItem is present in the list");} else {print("$searchItem is not present in the list");}}