To check if a specified element is present in an array in Swift, you can use methods like contains
.
In this example,
numbers
with values 10, 20, 30, 40, 50.contains
method to check if element 30 is present in the array.contains
method returns true if the element is found, otherwise false.let numbers = [10, 20, 30, 40, 50]
let isPresent = numbers.contains(30)
print("Element 30 is present:", isPresent)
Element 30 is present: true
In this example,
fruits
with values 'apple', 'banana', 'cherry'.contains
method to check if element 'banana' is present in the array.contains
method returns true if the element is found, otherwise false.let fruits = ["apple", "banana", "cherry"]
let isPresent = fruits.contains("banana")
print("Element 'banana' is present:", isPresent)
Element 'banana' is present: true
In this tutorial, we learned How to check if Specified Element is present in the Array in Swift language with well detailed examples.