# Swift Program to Find the Symmetric Difference 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 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()

Updated on 12-Oct-2022 07:20:41