Swift Set isSuperset()

The isSuperset() method returns true if a set has every element of another set (passed as an argument). If not, it returns false.

Example

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

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

isSuperset() Syntax

The syntax of the set isSuperset() method is:

set.isSuperset(otherSet)

Here, set is an object of the Set class.


isSuperset() Parameters

The isSuperset() method takes a single parameter:

  • otherSet - The set of elements.

isSuperset() Return Value

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

Example: Swift Set isSuperset()

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

// check if employees is super set of developers or not print(employees.isSuperset(of: developers))
// check if employees is super set of designers or not print(employees.isSuperset(of: designers))

Output

true
false

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

Since

  • employees is a superset of developers, the method returns true.
  • employees is not a subset of designers, 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