Swift Set isSubset()

The isSubset() method returns true if all elements of a set are present in another set (passed as an argument). If not, it returns false.

Example

var A: Set = [1, 2, 3]
var B: Set = [1, 2, 3, 4, 5]

// check if A is subset of B or not print(A.isSubset(of: B))
// Output: true

isSubset() Syntax

The syntax of the set isSubset() method is:

set.isSubset(otherSet)

Here, set is an object of the Set class.


isSubset() Parameters

The isSubset() method takes a single parameter:

  • otherSet - The set of elements.

isSubset() Return Value

  • The isSubset() method returns true if set is a subset of otherSet. If not, it returns false.

Example: Swift Set isSubset()

var employees: Set = ["Sabby", "Cathy", "Kenny", "Sammy", "Lanny"]
var developers: Set = ["Sabby", "Lanny"]
var designers: Set = ["Cathy", "Patty"]

// check if developer is subset of employees print(developers.isSubset(of: employees))
// check if developer is subset of employees print(designers.isSubset(of: employees))

Output

true
false

Here, we have used the isSubset() method to check if one set is a subset of another or not.

Since

  • developers is subset of employees, the method returns true.
  • designers is not a subset of employees, the method returns false.
Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community