# Swift Program to Find the Subtraction of Two Sets

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 find the subtraction of two sets

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.

Subtraction is one of the set operation which is used to create a set of elements that are present in first set but not in the second set. For example, we have two sets: set X and set Y, now we subtract set Y from set X. So the resultant set will contain elements that are present in set X. But not in set Y.

To find the subtraction of two sets Swift provide an in-built function named as subtracting(). This function returns a new set which contain the elements that are not present in the specified set.

Below is a demonstration of the same −

Suppose our given input is −

Set 1 = [34, 5, 78, 9, 10]
Set 2 = [34, 45, 5, 10, 100]


The desired output would be −

Subtracting Set = [9, 78]


## Syntax

Following is the syntax −

Set1.subtracting(Set2)


## Algorithm

Following is the algorithm −

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

• Step 2 − Finding the subtraction of two sets using subtracting() function and store the result into a variable −

var subSet = mySet1.subtracting(mySet2)


For sorted set −

var subSet = mySet1.subtracting(mySet2).sorted()

• Step 3 − Print the output

## Example 1

The following program shows how to subtracting two sets.

import Foundation
import Glibc

var mySet1 : Set = ["apple", "mango", "jackfruit"]
var mySet2 : Set = ["jackfruit", "kiwi", "mango", "dragon fruit"]

// Subtracting two sets
var subSet = mySet1.subtracting(mySet2)

print("Set 1:", mySet1)
print("Set 2:", mySet2)
print("Subtracting Set:", subSet)


## Output

Set 1: ["jackfruit", "mango", "apple"]
Set 2: ["jackfruit", "kiwi", "dragon fruit", "mango"]
Subtracting Set: ["apple"]


Here, in the above code, we have two sets: Set 1 and Set 2 of String type. Now we find their subtraction using subtracting() function −

var subSet = mySet1.subtracting(mySet2)


Hence the result is ["apple"].

## Example 2

The following program shows how to subtracting two sets.

import Foundation
import Glibc

var mySet1 : Set = [34, 2, 13, 10, 4, 11]
var mySet2 : Set = [98, 2, 10, 49, 23]

// Subtracting two sets
var subSet = mySet1.subtracting(mySet2).sorted()

print("Set 1:", mySet1)
print("Set 2:", mySet2)
print("Subtracting Set:", subSet)


## Output

Set 1: [34, 11, 13, 2, 10, 4]
Set 2: [2, 10, 49, 23, 98]
Subtracting Set: [4, 11, 13, 34]


Here, in the above code, we have two sets: Set 1 and Set 2 of integer type. Now we subtract them using subtracting() function. This function will return a set which contain only those elements that are present in Set 1 but not in the Set 2 that is [4, 11, 13, 34]. Here the result is sorted in ascending order because we use sorted() function with subtracting() function using dot operator −

var subSet = mySet1.subtracting(mySet2).sorted()

Updated on 12-Oct-2022 07:22:51