Swift Program to Find the Intersection of Two Sets

SwiftServer Side ProgrammingProgramming

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

Intersection is one of the set operation which is used to create a set of elements that are common in both the sets. For example, we have two sets: set X and set Y, now the intersection of both the sets contains only those elements that are common in both the sets.

To find the intersection of two sets Swift provide in-built function named as intersection(). This function returns a new set which contain the intersection 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 −

Intersection Set = [34, 5, 10]

Syntax

Following is the syntax −

Set1.intersection(Set2)

Algorithm

Following is the algorithm −

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

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

var intersectionSet = mySet1.intersection(mySet2)

For sorted set −

var intersectionSet = mySet1.intersection(mySet2).sorted()
  • Step 3 − Print the output

Example 1

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

import Foundation import Glibc var mySet1 : Set = ["apple", "mango", "jackfruit"] var mySet2 : Set = ["jackfruit", "kiwi", "mango", "dragon fruit"] // Finding intersection of two sets var intersectionSet = mySet1.intersection(mySet2) print("Set 1:", mySet1) print("Set 2:", mySet2) print("Intersection Set:", intersectionSet)

Output

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

Here, in the above code, we have two sets: Set 1 and Set 2 of String type. Now we find their intersection using intersection() function. This function will return a set which contain the intersection of both the sets −

var intersectionSet = mySet1.intersection(mySet2)

Hence the result is ["mango", "jackfruit"].

Example 2

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

import Foundation import Glibc var mySet1 : Set = [3, 45, 67, 2, 4] var mySet2 : Set = [34, 5, 65, 2, 3] // Finding intersection of two sets // Here the result will in sorted form var intersectionSet = mySet1.intersection(mySet2).sorted() print("Set 1:", mySet1) print("Set 2:", mySet2) print("Intersection Set:", intersectionSet)

Output

Set 1: [4, 3, 45, 67, 2]
Set 2: [2, 3, 5, 34, 65]
Intersection Set: [2, 3]

Here, in the above code, we have two sets: Set 1 and Set 2 of integer type. Now we find their intersection using intersection() function. This function will return a set which contain only the common elements present in both the sets that is [2, 3]. Here the result is sorted in ascending order because we use sorted() function with intersection() function using dot operator −

var intersectionSet = mySet1.intersection(mySet2).sorted()
raja
Updated on 12-Oct-2022 07:18:07

Advertisements