Swift Set isDisjoint()

The isDisjoint() method returns true if two sets are disjoint sets. If not, it returns false.

Example

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

// check if A is disjoint with B or not print(A.isDisjoint(with: B))
// Output: true

isDisjoint() Syntax

The syntax of the set isDisjoint() method is:

set.isDisjoint(otherSet)

Here, set is an object of the Set class.


isDisjoint() Parameters

The isDisjoint() method takes a single parameter:

  • otherSet - The set of elements.

isDisjoint() Return Value

  • The isDisjoint() method returns true if set is disjoint with otherSet. If not, it returns false.

Example: Swift Set isDisjoint()

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

// check if A and B are disjoint or not print("Are A and B disjoint?", A.isDisjoint(with: B))
// check if A and C are disjoint or not print("Are A and C disjoint?", A.isDisjoint(with: C))

Output

Are A and B disjoint? true
Are A and C disjoint? false

Here, we have used the isDisjoint() method to check if two sets are disjoint or not.

Since

  • A and B have unique elements, the method returns true.
  • A and C both have element 4, 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