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?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges