# Swift Program to Check if the given Set is the Subset of Another Set

SwiftServer Side ProgrammingProgramming

#### Swift 4 Online Training

38 Lectures 1 hours

#### Swift Basics: Learn to Code from Scratch [For Beginners]

13 Lectures 2 hours

#### How to create Tic-Tac-Toe Game for iPhone using Swift in Xcode - iOS App

7 Lectures 1 hours

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

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

Below is a demonstration of the same −

Suppose our given input is −

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


The desired output would be −

YES! Set 1 is the subset of Set 2


## Syntax

Following is the syntax −

Set1.isSubset(of: Set2)


## Algorithm

Following is the algorithm −

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

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

var subSet = mySet1.isSubset(of:mySet2)

• Step 3 − If subSet = 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 subset of another set.

import Foundation
import Glibc

var mySet1 : Set = [34, 5, 9]
var mySet2 : Set = [34, 5, 78, 9, 10]

print("Set 1 = ",mySet1)
print("Set 2 =",mySet2)

// Checking if the mySet1 is the subset of mySet2
var subSet = mySet1.isSubset(of:mySet2)
if (subSet == true){
print("mySet1 is the subset of mySet2")
} else{
print("mySet1 is not the subset of mySet2")
}


## Output

Set 1 = [34, 5, 9]
Set 2 = [5, 10, 9, 78, 34]
mySet1 is the subset of mySet2


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

## Example 2

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

import Foundation
import Glibc

var mySet1 : Set = [34, 5, 9]
var mySet2 : Set = [34, 5, 78, 9, 10]
var mySet3 : Set = [5, 9]

// Checking if the mySet1 is the subset of mySet2
var subSet1 = mySet1.isSubset(of:mySet2)

// Checking if the mySet1 is the subset of mySet3
var subSet2 = mySet1.isSubset(of:mySet3)

// Checking if the mySet3 is the subset of mySet1
var subSet3 = mySet3.isSubset(of:mySet1)

print("Is mySet1 is the subset of mySet2?:", subSet1)
print("Is mySet1 is the subset of mySet3?:", subSet2)
print("Is mySet3 is the subset of mySet1?:", subSet3)


## Output

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


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

var subSet1 = mySet1.isSubset(of:mySet2) // Return true
var subSet2 = mySet1.isSubset(of:mySet3) // Return false
var subSet3 = mySet3.isSubset(of:mySet1) // Return true

Updated on 12-Oct-2022 07:24:55