- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Swift Program to Find the Subtraction of Two Sets

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()

- Related Questions & Answers
- PHP program to calculate the repeated subtraction of two numbers
- Java Program to Calculate the intersection of two sets
- Python Program to find Duplicate sets in list of sets
- Java Program to Calculate union of two sets
- Java Program to compare two sets
- 8086 program to determine subtraction of corresponding elements of two arrays
- Java Program to Calculate the difference between two sets
- Program to find maximum sum of two sets where sums are equal in C++
- JavaScript subtraction of two float values?
- Get the intersection of two sets in Java
- Get the union of two sets in Java
- C Program for subtraction of matrices
- Get the asymmetric difference of two sets in Java
- Cartesian product of two sets in JavaScript
- Finding union of two sets in JavaScript
- Python Program to find out the number of sets greater than a given value