How to check if an element exists in an array in Swift
In this post we will give you information about How to check if an element exists in an array in Swift. Hear we will give you detail about How to check if an element exists in an array in SwiftAnd how to use it also give you demo for it if it is necessary.
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"
Hope this code and post will helped you for implement How to check if an element exists in an array in Swift. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs