Swift Program to Merge Two Sets


Merging two sets means combining all the elements of a set into another set without any duplicates. In Swift, we can either use formUnion() function or for-in loop to merge the elements of two sets. Lets discuss both the methods in detail along with examples.

Method 1: Using formUnion() Function

To merge two sets we can use formUnion() function. The formUnion() function is used to insert the elements of the given sequence into the set. Here the sequence can be anything a set, array, etc. This function merge two sets without any duplicates.

Syntax

set1.formUnion(set2)

Where set1 and set2 are two finite sets of same type. The formUnion() function insert all the element of set2 into set1. If both the sets contains some duplicates, then in the resultant set it will remove duplicate elements. Also this function does not return any value, it simple combine the elements of the set2 into set1.

Example

In the following example, we will create and initialise two sets named as ‘mSet’ and ‘nSet’. Now we merge nSet into mSet using formUnion() function and display output.

import Foundation
import Glibc

// Creating sets
var mSet: Set = [43, 12, 6, 2, 8]
var nSet: Set = [10, 4, 51, 8, 2]

print("Original Sets:")
print("mSet:", mSet)
print("nSet:", mSet)

// Merging two sets
mSet.formUnion(nSet)

print("Resultant set:", mSet)

Output

Original Sets:
mSet: [2, 12, 6, 8, 43]
nSet: [2, 12, 6, 8, 43]
Resultant set: [51, 43, 12, 10, 4, 2, 8, 6]

Method 2: Using for-in Loop

We can also merge two sets using for-in loop along with insert() function. In this method, for-in loop is used to iterate through each element of SetA and then use insert() function to insert elements into SetB. It also merge two sets without any duplicates.

Example

In the following example, we will create and initialise two sets named as ‘myColorSet1’ and ‘myColorSet2’. Then we run a for loop which iterate through each element in ‘myColorSet2’ and then insert current element in ‘myColorSet1’ set in each iteration using insert() function. After completing the for-in loop we display the updated ‘myColorSet1’ set.

import Foundation
import Glibc

// Creating sets
var myColorSet1: Set = ["Blue", "Pink", "Green"]
var myColorSet2: Set = ["Pink", "Black", "White", "Orange", "Brown"]

print("Original Sets:")
print("myColorSet1:", myColorSet1)
print("myColorSet2:", myColorSet2)

// Merging two sets
for myEle in myColorSet2 {
   myColorSet1.insert(myEle)
}

print("Resultant set:", myColorSet1)

Output

Original Sets:
myColorSet1: ["Blue", "Green", "Pink"]
myColorSet2: ["Black", "Pink", "Orange", "Brown", "White"]
Resultant set: ["Brown", "Blue", "Green", "White", "Pink", "Black", "Orange"]

Conclusion

So this is how we can merge two sets. Although Swift does not provide any direct method to merge two sets, but we can achieve our goal using formUnion() function and for-in loop very well.

Updated on: 21-Apr-2023

694 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements