Swift Program to Initializing a Set


In Swift, a set is used to define a collection of unique elements, means a set doesn’t contains duplicate elements. In a set, the elements are not arranged in a particular order. Initialising a set means to store some information in the set. It can be done at the time of declaration or after declaration. We can initialise a set in various way like initialising a set without explicitly passing data types, initialising a set with explicitly passing data types, and using inbuilt insert() function. Now lets discuss all the method in detail along with examples.

Method 1

In this method, we are initialising a set at the time of declaration without passing a data type to the set explicitly. In this situation, the compiler will automatically determine the type of the set according to the stored value.

Syntax

var newset: Set = [ele1, ele2, ele3, ele4,……]

Where newset is the name of the set, Set keywords is used to create a set, and ele1, ele2, ele3, etc., represent the elements of the set.

Example

In the following swift example, we will create and initialise three sets of different types without explicitly passing their data types and display the output.

import Foundation
import Glibc

// Initialising the set of string 
let color: Set = ["Blue", "Green", "Orange", "Pink", "Purple"]

// Initialising the set of integer
let marks: Set = [34, 29, 23, 56, 12, 10]

// Initialising the set of characters
let luckyChar: Set = ["A", "R", "T", "Q", "M"]

print(color)
print(marks)
print(luckyChar)

Output

["Green", "Orange", "Blue", "Pink", "Purple"]
[12, 56, 23, 10, 29, 34]
["M", "T", "R", "Q", "A"]

Method 2

In this method, we are initializing a set at the time of declaration by passing its data type explicitly in between angular brackets<>.

Syntax

var newset: Set<data_type> = [ele1, ele2, ele3, ele4,……]

Where newset is the name of the set, Set keywords is used to create a set along with data type, and ele1, ele2, ele3, etc., represent the elements of the set.

Example

In the following swift example, we will create and initialise three sets of different types with explicitly passing their data types and display the output.

import Foundation
import Glibc

// Initialising the set of string 
let color: Set<String> = ["Blue", "Black", "Peach", "Brown", "Purple"]

// Initialising the set of integer
let marks: Set<Int> = [34, 89, 23, 19, 44, 21]

// Initialising the set of characters
let luckyChar: Set<Character> = ["V", "R", "W", "Q", "Z"]

print(color)
print(marks)
print(luckyChar)

Output

["Purple", "Blue", "Black", "Brown", "Peach"]
[89, 34, 21, 23, 19, 44]
["V", "W", "Q", "Z", "R"]

Method 3

Swift provide an inbuilt function named as insert() to initialise a set. This function is used to insert the given element in the set.

n this method, we are initialising a set at the time of declaration by passing its data type explicitly in between angular brackets<>.

Syntax

func insert(element)

Where insert() function only takes one parameter that is a new element and add this new element into the set. If the new element is already present in the set, then it does not change the specified set.

Example

In the following swift example, we will first create an empty set of string type. Then we add new elements using insert() function and then display the output.

import Foundation
import Glibc

// Declaring a set of string 
var veggies = Set<String>()

// Initialising set 
veggies.insert("Potato")
veggies.insert("Onion")
veggies.insert("Eggplant")
veggies.insert("Pea")
veggies.insert("Pumpkin")

// Displaying set
print(veggies)

Output

["Potato", "Eggplant", "Onion", "Pea", "Pumpkin"]

Conclusion

So this is how we can simply initialise a set. Using the above discussed methods we can create and initialise a set of any data type with any number of elements.

Updated on: 21-Apr-2023

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements