# Swift Program to Find the Union 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 union 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.

Union is one of the set operation which is used to create a set of all the elements that are present in the given two sets except duplicate elements. For example, we have two sets: set X and set Y, now the union of both the sets contains all the elements present on set X and set Y except for the duplicate elements.

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

Below is a demonstration of the same −

Suppose our given input is −

Set 1 = [3, 45, 67, 86, 4]
Set 2 = [34, 5, 65, 2, 3]


The desired output would be −

Union Set = [5, 34, 2, 45, 65, 4, 67, 3, 86]


## Syntax

Following is the syntax −

Set1.union(Set2)


## Algorithm

Following is the algorithm −

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

• Step 2 − Finding union of two sets using union() function −

var unionSet = mySet1.union(mySet2)


For sorted set −

var unionSet = mySet1.union(mySet2).sorted()

• Step 3 − Print the output

## Example 1

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

import Foundation
import Glibc

var mySet1 : Set = [23, 4, 56, 7, 8]
var mySet2 : Set = [78, 23, 10, 87, 8, 4]

// Finding union os two sets
var unionSet = mySet1.union(mySet2)

print("Set 1:", mySet1)
print("Set 2:", mySet2)
print("Union Set:", unionSet)


## Output

Set 1: [56, 8, 7, 23, 4]
Set 2: [10, 87, 8, 78, 23, 4]
Union Set: [10, 7, 4, 78, 8, 87, 56, 23]


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

var unionSet = mySet1.union(mySet2)


Hence the result is [10, 7, 4, 78, 8, 87, 56, 23].

## Example 2

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

import Foundation
import Glibc

var mySet1 : Set = [23, 4, 56, 7, 23]
var mySet2 : Set = [78, 23, 3, 7, 800, 4]

// Finding union of two sets
// Here the result will in sorted form
var unionSet = mySet1.union(mySet2).sorted()

print("Set 1:", mySet1)
print("Set 2:", mySet2)
print("Union Set:", unionSet)


## Output

Set 1: [23, 56, 4, 7]
Set 2: [4, 3, 23, 7, 800, 78]
Union Set: [3, 4, 7, 23, 56, 78, 800]


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

var unionSet = mySet1.union(mySet2).sorted()

Updated on 12-Oct-2022 07:15:02