VOOZH about

URL: https://www.geeksforgeeks.org/swift/how-to-check-if-the-set-contains-a-given-element-in-swift/

⇱ How to check if the set contains a given element in Swift? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to check if the set contains a given element in Swift?

Last Updated : 2 Jun, 2022

Swift supports the generic collection and set is one of them. A set is used to store unordered values of the same type. It means you are not allowed to store different types in the set, e.g. a set is of int type then you can only store values of int type not of string type. A is used set instead of an array if the order of the values is not defined or you want to store unique values. Set doesn't keep duplicate values, it always keeps unique values and uses a hash table to store the elements. In the Swift Set, we can easily check if the given set contains the specified element to not. To do this task we use the contains() function. This function is used to check if the given element is present or not in the specified set. It will return true if the given value is present in the set otherwise it will return false. This function is case sensitive, here Mohan and mohan are two different words.

Syntax:

setName.contains(ele)

Here,

setName is the object of the set class and the parameter ele represent the element. 

Return Value: This function will return true if the given array contains the specified value otherwise it will return false. 

Example 1:

Output:

1 is not present in the set
67 is present in the set

Example 2: 

Output:

true
false
Comment
Article Tags:

Explore