Swift Program to Remove a Subset from a Set


In Swift, a set is used to create an unordered collection of unique elements. To remove a subset from a set Swift provide two insult functions named as subtract() and subtracting(). Lets discuss both the methods in detail along with examples.

Method 1: Using subtract(_:) Function

The subtract(_:) function is used to remove the given subset from the specified set.

Syntax

func subtract(_subset:mSet<Elements>)

Where mSet is the subset which we want to remove from the given set and it must be a finite set. This function return a set after removing the common elements of set and subset.

Example

In the following example, we will first create two sets of integer type named as mySet and mSubset. Where mSubset is the set which we want to remove from the given set. Then we remove mSubset from the mySet using subtract(_:) function and then display the updated set.

import Foundation
import Glibc

// Creating set and subset
var mySet: Set<Int> = [10, 40, 20, 60, 30, 50]
let mSubset: Set<Int> = [20, 60, 30]

print("Original Set:", mySet)

// Removing subset from a set
mySet.subtract(mSubset)

print("Updated Set:", mySet)

Output

Original Set: [10, 40, 20, 60, 30, 50]
Updated Set: [10, 40, 50]

Method 2: Using subtracting(_:) Function

Swift provide an inbuilt function named as subtracting(_:) to remove a subset from a set. This function returns a new set which containing only those elements from the original set that are not present in the given subset.

Syntax

func subtract(_subset:mySet<Elements>)

Where mySet is the subset which we want to remove from the given set and it must be a finite set. This function return a new set in which only those elements are present that are not available in specified subset.

Example

In the following swift example, we will first create two sets of string type named as myVeggies and subset. Where subset is the set which we want to remove from the given set. Then we remove subset from the myVeggies set using subtracting(_:) function and store the result into new set. Then display the new set.

import Foundation
import Glibc

// Creating a set and a subset
let myVeggies: Set<String> = ["Potato", "Chilli", "Eggplant", "Spinach", "Tomato", "Beans"]
let subset: Set<String> = ["Chilli", "Beans"]

print("Original Set:", myVeggies)

// Removing subset from a set
let myNewSet = myVeggies.subtracting(subset)

print("New Set:", myNewSet)

Output

Original Set: ["Tomato", "Beans", "Chilli", "Eggplant", "Spinach", "Potato"]
New Set: ["Spinach", "Potato", "Tomato", "Eggplant"]

Conclusion

So this is how we can remove s subset from a set. The working of subtract(_:) and subtracting(_:) functions are quite similar, but the major difference is subtract(_:) function modifies the original set whereas subtracting(_:) function create a new resultant set. Also, don’t get confused if the order of set elements change because set is an unordered collection.

Updated on: 24-Apr-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements