Swift Set contains()

The contains() method checks whether the specified element is present in the set or not.

Example


contains() Syntax

The syntax of the set contains() method is:

set.contains(obj)

Here, set is an object of the set class.


contains() Parameters

The contains() method takes a single parameter:

  • obj - element to be checked for its presence in the set

contains() Return Values

The contains() method returns:

  • true - if the set contains the specified element
  • false - if the set doesn't contain the specified element

Example 1: Swift String contains()

Output

true
false

In the above example,

  • "Nadal" is present in the set, so the method returns true.
  • "Federer" is not present in the set, so the method returns false.

Example 2: Using contains() With if...else

Output

set contains Gregory
set doesn't contain gregory