Swift Program to Convert Set of String to Array of String


In this article, we will learn how to write a swift program to convert set of string to array of string.

An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. Now to convert a set of string to an array of string we use the following two methods −

  • Using Array() initializer

  • Using map() function

Method 1: Using Array() Initializer

To convert a set of string to an array of string we can use Array() initializer. It returns an array created from the given sequence.

Syntax

Array(InputValue)

Here, InputValue is a sequence which we want to convert into an array.

Algorithm

  • Step 1 − Create a set of string.

  • Step 2 − Convert the set of string into array of string using Array() initializer

  • Step 3 − Find the type of the resultant variable.

  • Step 4 − Print the output.

Example

Following Swift program to convert set of string to array of string.

import Foundation
import Glibc

// Creating a set of string
let strSet: Set = ["Cow", "Cat", "Dog", "Bee", "Bird","Ants"]
print("Set:", strSet)
print("Type:", type(of: strSet))

// Converting set of string into set of an array
var strArray = Array(strSet)
print("\nArray:", strArray)
print("Type:", type(of: strArray))

Output

Set: ["Cow", "Cat", "Dog", "Ants", "Bee", "Bird"]
Type: Set<String>

Array: ["Cow", "Cat", "Dog", "Ants", "Bee", "Bird"]
Type: Array<String>

Here in the above code, we have a set of string. Now to convert set into array we pass the given set to the Array() initializer. Then this initialiser return an array of string.

Method 2: Using map() function

The can use map() function to convert a given set of string to an arr ay of string. The map() function is used to get an array which contains the result fo mapping the given closure over the given collections s elements.

Algorithm

  • Step 1 − Create a set of string.

  • Step 2 − Convert the set of string into array of string using map() function.

  • Step 3 − Find the type of the resultant variable.

  • Step 4 − Print the output.

Example 1

Following Swift program to convert set of string to array of string.

import Foundation
import Glibc

// Creating a set of string
let strSet: Set<String> = ["Toffee","Candy","Chocolate","Bites","Lollipop"]
print("Set:", strSet)
print("Type:", type(of: strSet))

// Converting set of string into set of an array
var strArray = strSet.map{return $0}
print("\nArray:", strArray)
print("Type:", type(of: strArray))

Output

Set: ["Candy", "Toffee", "Lollipop", "Chocolate", "Bites"]
Type: Set<String>

Array: ["Candy", "Toffee", "Lollipop", "Chocolate", "Bites"]
Type: Array<String>

Here in the above code, we have a set of string. Now we convert the set of string into the array of string using map() function. Here this map() function return a string from the set starting from index 0 and store the result into a variable in the form of an array.

Example 2

Following Swift program to convert set of string to array of string.

import Foundation
import Glibc

// Creating a set of string
let strSet: Set<String> = ["Blue","Yellow","Pink","Black","Orange","Red","Green"]
print("Set:", strSet)
print("Type:", type(of: strSet))

// Converting set of string into set of an array
// in sorted form
var strArray = strSet.sorted().map{return $0}
print("\nArray:", strArray)
print("Type:",type(of: strArray))

Output

Set: ["Orange", "Green", "Black", "Blue", "Yellow", "Pink", "Red"]
Type: Set<String>

Array: ["Black", "Blue", "Green", "Orange", "Pink", "Red", "Yellow"]
Type: Array<String>

Here in the above code, we have a set of string. Now we convert the set of string into the sorted array of string. So to do this first we use map() function which will return strings from the set starting from index 0 and then we sort them in alphabetical order using sorted() function and finally store the result into a variable in the form of an array.

Conclusion

So this is how we can convert a set of string into an array of string. Here, the order of the elements of the resultant array may not be same as the original set because set does not have a defined order for their elements.

Updated on: 17-Jan-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements