# Swift Program to Search an Element in a Set

In Swift, to search elements in a set we can use either contains(_:) function or linear search. Both the methods efficiently search an element in a set. Lets discuss both the methods in detail along with examples.

## Method 1: Using contains(_:) Function

Swift provide an inbuilt function named as contains() function to search an element in a set. This function return true if the specified element is present in the given set. This function will return false if the specified element is present in the given set.

### Syntax

sets.contains(myElement)


Where sets is a finite set and myElement represent the target element which we want to search in sets. This function return a boolean value which indicates that the given element is present in the set or not.

### Algorithm

• Step 1 − Create and initialise a set.

• Step 2 − Create a variable named as ele = “Tomato to store the element which we want to reach in the set.

• Step 3 − Now check if the set contains ele using contains() function.

• Step 4 − If the set contains ele, then print "Yes! veggies set contains ‘Tomato'".

• Step 5 − If the set does not contains ele, then print "No! veggies set does not contain ‘Tomato’".

### Example

In the following swift example, we will create and initialise a set named as ‘veggies’. Then we create a variable named ‘ele’ to store the element which we want to search in the veggies set. Then we use contains(_:) function to search ‘Tomato’ element in the veggies set. If the contains(_:) function returns true, then we print "Yes! veggies set contains 'Tomato'". Otherwise, we print "No! veggies set does not contain 'Tomato'". Here ‘Tomato’ is present in the given set so we get the "Yes! veggies set contains 'Tomato'" message.

import Foundation
import Glibc

// Creating a set
var veggies: Set<String> = ["Pea", "Onion", "Potato", "Tomato", "Chilli"]

// Element to be searched
var ele = "Tomato"

// Search ele in veggies set
if veggies.contains(ele) {
print("Yes! veggies set contains 'Tomato'")
} else {
print("No! veggies set does not contain 'Tomato'")
}


### Output

Yes! veggies set contains 'Tomato'


## Method 2: Using Linear Search

To search an element in a set we can also use linear search. In linear search, we search element sequentially from the start until the desired element is found. If the element is not found, then return nil.

### Algorithm

• Step 1 − Create and initialise a set.

• Step 2 − Create a variable named as ele = 18 to store the element which we want to reach in the set.

• Step 3 − Create another variable named as flag to keep the track of whether the element is found or not. The flag variable is set to false. If the element is found, then the value of flag is changed to true. Otherwise not.

• Step 4 − Then run a for-in loop to iterate through each element of the set and check if the current element is equal to the ele. If the match found, then set the flag to true and break the loop.

• Step 5 − Finally we check the value of flag.

• Step 6 − If flags is true print "YES! set contains ‘\(ele)’”.

• Step 7 − If flags is false print "NO! set does not contain ‘\(ele)'".

### Example

In the following swift example, we will create and initialise a set named as ‘myNum’. Then we create a variable named as ‘ele’ to store the element, which we want to search in the myNum, set. We also create a flag variable, which will keep the track of whether the element is present in the set, or not. Then we run a for loop to iterate through each element of the given set and check if the specified element is present or not. If the element found, then set the value of flag variable to true and break the loop. Now we check the value of flag variable and display message accordingly. If the value of flag is true then print "YES! set contains ‘\(ele)’". Otherwise print "NO! set does not contain ‘\(ele)’". Here in our case 18 is present in the set so we get “YES! set contains ’18’" message.

import Foundation
import Glibc

// Creating a set
var myNum: Set = [23, 14, 2, 18, 22, 15, 16]

// Element to be searched
let ele = 18
var flag = false

// Search ele in myNum set
for element in myNum {
if element == ele {
flag = true
break
}
}

if flag {
print("YES! set contains '\(ele)'")
} else {
print("NO! set does not contain '\(ele)'")
}


### Output

YES! set contains '18'


## Conclusion

So this is how we can search an element in a set. Both contains() function and linear search methods return accurate result. Here using contains() function is the less time taking method to search an element in the Set as compare to linear search.

Updated on: 24-Apr-2023

117 Views 