Swift Program to Check if the given Set is the Disjoint of Another Set

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to check if the given set is the disjoint of another set.

Set is a primary collection type in Swift. It is an unordered collection which stores unique values of same data type. You are not allowed to store different type of values in the same set. A set can be mutable or immutable.

Suppose we have two sets: Set X and Set Y. Now a Set X is said to the disjoint of Set Y if both the sets does not contain any common element.

To check if the given set is the disjoint of another set Swift provide an in-built function named isDisjoint(). This function will return true if the given set is the disjoint of another set. Otherwise it will return false.

Below is a demonstration of the same −

Suppose our given input is −

Set 1 = [10, 67, 92, 98, 23]
Set 2 = [938, 100, 38, 12]	

The desired output would be −

YES! Set 1 is the disjoint of Set 2	

Syntax

Following is the syntax −

Set1.isDisjoint(with: Set2)

Algorithm

Following is the algorithm −

  • Step 1 − Declare and initialise two sets with values.

  • Step 2 − Check if the mySet1 is the superset of mySet2 using isSuperset() function and store the result into a variable −

var subSet = mySet1.isSuperset(of:mySet2)	
  • Step 3 − If supSet = true the print "mySet1 is the subset of mySet2".

  • Step 4 − Otherwise print "mySet1 is not the subset of mySet2".

Example 1

The following program shows how to check if the given set is the disjoint of another set.

import Foundation import Glibc var mySet1 : Set = [34, 5, 78, 9, 10] var mySet2 : Set = [83, 1, 39, 23, 21] // Checking if the mySet1 is the disjoint of mySet2 var subSet = mySet1.isDisjoint(with:mySet2) if (subSet == true){ print("mySet1 is the disjoint of mySet2") } else{ print("mySet1 is not the disjoint of mySet2") }

Output

mySet1 is the superset of mySet2

Here, in the above code, we have two sets: mySet1 and mySet2. Now using isDisjoint() function we check if mySet1 is the disjoint of mySet2. So we get output: "mySet1 is the disjoint of mySet2" because both the set does not have common elements.

Example 2

The following program shows how to check if the given set is the disjoint of another set.

import Foundation import Glibc var mySet1 : Set = ["pig", "dog", "snake", "owl"] var mySet2 : Set = ["cat", "bat", "otter"] var mySet3 : Set = ["owl", "dog"] // Checking if the mySet1 is the disjoint of mySet2 var disSet1 = mySet1.isDisjoint(with:mySet2) // Checking if the mySet1 is the disjoint of mySet3 var disSet2 = mySet1.isDisjoint(with:mySet3) // Checking if the mySet3 is the disjoint of mySet2 var disSet3 = mySet3.isDisjoint(with:mySet2) print("Is mySet1 is the disjoint of mySet2?:", disSet1) print("Is mySet1 is the disjoint of mySet3?:", disSet2) print("Is mySet3 is the disjoint of mySet2?:", disSet3)

Output

Is mySet1 is the superset of mySet2?: true
Is mySet1 is the superset of mySet3?: false
Is mySet3 is the superset of mySet1?: true

Here, in the above code, we have three sets: mySet1, mySet2, and mySet3. Now using isDisjoint() function we check for disjoint −

var supSet1 = mySet1.isSuperset(of:mySet2) // Return true
var supSet2 = mySet1.isSuperset(of:mySet3) // Return false
var supSet3 = mySet3.isSuperset(of:mySet1) // Return true
raja
Updated on 12-Oct-2022 07:34:36

Advertisements