- 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 Symmetric Difference of Two Sets

This tutorial will discuss how to write swift program to find the symmetric difference 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.

Symmetric difference is one of the set operation which is used to create a set of elements that are present in either of two sets but not in both sets. For example, we have two sets: set X and set Y, now the symmetric difference of both the sets contains only those elements that unique in both set X and set Y. It exclude common elements present in both the sets.

To find the symmetric difference of two sets Swift provide in-built function named as symmetricDifference(). This function returns a new set which contain the symmetric difference of given two sets.

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 −

Symmetric Difference Set = [78, 9, 45, 100]

## Syntax

Following is the syntax −

Set1.symmetricDifference(Set2)

## Algorithm

Following is the algorithm −

**Step 1**− Declare and initialise two sets with values.**Step 2**− Finding the symmetric difference of two sets using symmetricDifference() function and store the result into a variable −

var symDiffSet = mySet1.symmetricDifference(mySet2)

For sorted set −

var symDiffSet = mySet1.symmetricDifference(mySet2).sorted()

**Step 3**− Print the output

## Example 1

The following program shows how to find the symmetric difference of two sets.

import Foundation import Glibc var mySet1 : Set = ["apple", "mango", "jackfruit"] var mySet2 : Set = ["jackfruit", "kiwi", "mango", "dragon fruit"] // Finding symmetric difference of two sets var symDiffSet = mySet1.symmetricDifference(mySet2) print("Set 1:", mySet1) print("Set 2:", mySet2) print("Symmetric Difference Set:", symDiffSet)

## Output

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

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

var symDiffSet = mySet1.symmetricDifference(mySet2)

Hence the result is ["dragon fruit", "apple", "kiwi"].

## Example 2

The following program shows how to find the symmetric difference of two sets.

import Foundation import Glibc var mySet1 : Set = [312, 98, 32, 2, 87, 1, 98] var mySet2 : Set = [45, 90, 53, 2, 312, 1] // Finding symmetric difference of two sets // Here the result is sorted var symDiffSet = mySet1.symmetricDifference(mySet2).sorted() print("Set 1:", mySet1) print("Set 2:", mySet2) print("Symmetric Difference Set:", symDiffSet)

## Output

Set 1: [98, 312, 2, 87, 32, 1] Set 2: [45, 1, 2, 53, 312, 90] Symmetric Difference Set: [32, 45, 53, 87, 90, 98]

Here, in the above code, we have two sets: Set 1 and Set 2 of integer type. Now we find their symmetric difference using symmetricDifference() function. This function will return a set which contain only the unique elements present in both the sets that is [32, 45, 53, 87, 90, 98]. Here the result is sorted in ascending order because we use sorted() function with symmetricDifference() function using dot operator −

var symDiffSet = mySet1.symmetricDifference(mySet2).sorted()

- Related Questions & Answers
- Find the Symmetric difference between two arrays - JavaScript
- Java Program to Calculate the difference between two sets
- Python Pandas - Compute the symmetric difference of two Index objects
- Get the asymmetric difference of two sets in Java
- 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
- Python Pandas - Compute the symmetric difference of two Index objects and unsort the result
- Program to find maximum sum of two sets where sums are equal in C++
- C program to find sum and difference of two numbers
- Get the intersection of two sets in Java
- Get the union of two sets in Java
- Python program to find difference between two timestamps
- Cartesian product of two sets in JavaScript
- Finding union of two sets in JavaScript