Swift Program to Print 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. To print a set we can use for-in loop, forEach() function, as well as the name of the set. Lets discuss all the methods one by one in detail along with examples.

Method 1: Name of the Set

To print a set we simply call the name of the set in the print() function. It will display set elements in the square bracket([]). Or we can say that it will display set elements the way they are initialise(expect their order).

Syntax

print(newSet)

In the above syntax we will print a set ,where newSet is the name of the set.

Example

In the following example, we will create and initialise a set. Now we print the set using its name.

import Foundation
import Glibc

// Creating a set of integer
var myLuckyNumber : Set = [34, 2, 12, 67, 3, 8, 23, 9, 1] 

// Displaying set
print(myLuckyNumber)

Output

[9, 2, 1, 8, 12, 67, 34, 23, 3]

Method 2: Using for-in Loop

We can also print the elements of set using for-in loop. The for-in loop iterate through each element of the set and display them on the screen one by one.

Syntax

for x in newSet{
   print(x)
}

Where newset is the name of the set, and x variable store the current element from the newest in each iteration.

Example

In the following example, we will create and initialize a set of integer type. Then we use for-in loop to iterate through each element of the set and display them on the output screen.

import Foundation
import Glibc

// Creating a set of integer
var myLuckyNumber : Set = [3, 8, 23, 9, 1, 7, 12, 29] 

// Displaying set
for x in myLuckyNumber{
   print(x)
}

Output

8
12
1
23
7
29
3
9

Method 3: Using forEach() Function

To print a set Swift provide an inbuilt function named as forEach() function. The forEach() function calls the specified closure on each element present in the given set or sequence. Its working is same as the for-in loop.

Syntax

func forEach(_myclo:)

Where myclo is the closure which takes an element of the given sequence or set as a parameter and return the result according to the statement present in the closure.

Example

In the following example, we will first create and initialize a set, then display the elements of the set using forEach() function. In the forEach() function, we pass a closure({myRes in print(myRes)}) which will display all the elements of the set.

import Foundation
import Glibc

// Creating a set of integer
var myFavfruit : Set = ["Orange", "Black Grapes", "Peaches", "Apple", "Kiwi", "Mango"] 

// Displaying set
myFavfruit.forEach{
   myRes in print(myRes)
}

Output

Orange
Apple
Mango
Kiwi
Black Grapes
Peaches

Conclusion

So this is how we can simply print the set. All the discussed method display all the elements of set except the order of the set. So don’t get confused if the order of set elements change.

Updated on: 24-Apr-2023

251 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements