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

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to check if the given set is the superset 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 superset of Y if all the items of Set Y is also available in Set X.

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

Below is a demonstration of the same −

Suppose our given input is −

Set 1 = [984, 20, 84, 876, 3]
Set 2 = [3, 20, 876]

The desired output would be −

YES! Set 1 is the superset of Set 2	

Syntax

Following is the syntax −

Set1.isSuperset(of: 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 superset of another set.

import Foundation import Glibc var mySet1 : Set = [34, 5, 78, 9, 10] var mySet2 : Set = [34, 5, 9] // Checking if the mySet1 is the superset of mySet2 var subSet = mySet1.isSuperset(of:mySet2) if (subSet == true){ print("mySet1 is the superset of mySet2") } else{ print("mySet1 is not the superset of mySet2") }

Output

mySet1 is the superset of mySet2

Here, in the above code, we have two sets: mySet1 and mySet2. Now using isSuperset() function we check if mySet1 is the superset of mySet2. So we get output: "mySet1 is the superset of mySet2" because all the elements of mySet2 are present in mySet1.

Example 2

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

import Foundation import Glibc var mySet1 : Set = ["pig", "dog", "snake", "owl", "cat", "bat"] var mySet2 : Set = ["pig", "owl", "otter"] var mySet3 : Set = ["owl", "dog"] // Checking if the mySet1 is the superset of mySet2 var supSet1 = mySet1.isSuperset(of:mySet2) // Checking if the mySet1 is the superset of mySet3 var supSet2 = mySet1.isSuperset(of:mySet3) // Checking if the mySet3 is the superset of mySet1 var supSet3 = mySet3.isSuperset(of:mySet1) print("Is mySet1 is the superset of mySet2?:", supSet1) print("Is mySet1 is the superset of mySet3?:", supSet2) print("Is mySet3 is the superset of mySet1?:", supSet3)

Output

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

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

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

Advertisements