How to check if an element exists in an array in Swift
In this tutorial, we are going to learn about how to check if a specified element exists in an array in Swift.
Checking element exists
To check if a specified element exists or not in an array, we can use the built-in contains()
method in Swift.
The contains()
method returns true if an element is found in the array; otherwise it returns false.
Here is an example:
let prices = [10, 15, 20, 25]
if prices.contains(20){
print("20 is found")
}else{
print("20 is not found")
}
Output:
"20 is found"