# Swift Program to Get a Random Element from the Set

This tutorial will discuss how to write swift program to get a random element from the Set.

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.

### Syntax

Following is the syntax for Set −

var name : Set = [element1, element2, element3]


To get a random element from the Set Swift provide an in-built function named randomElement(). This function return a random element from the given Set. Here the returned object of type Optional<T>. So we type cast the value into the required datatype. If the given Set is empty, then it will return nil.

Below is a demonstration of the same −

Input

Suppose our given input is −

MySet = [34, 5, 6, 78, 12, 3]


Output

The desired output would be −

Random Number = 12


### Syntax

Following is the syntax −

SetName.randomElement()


### Algorithm

Following is the algorithm −

• Step 1 − Declare and initialise a Set with value.

• Step 2 − Find the random element using randomElement() −

var randEle = myNum.randomElement()!


Here this function will return value in Optional type so you can use either ! Or let to type cast into required datatypes.

• Step 3 − Print the output

### Example 1

The following program shows how to get a random element from the set.

import Foundation
import Glibc

// Creating a Set of string Type
var myNum: Set = [3, 56, 1, 98, 23, 4]

// Getting random elements
// Here we use ! To type cast the
// value into the integer type
var randEle = myNum.randomElement()!

print("Set: ", myNum)
print("Random Element: ", randEle)


### Output

Set: [56, 4, 1, 98, 23, 3]
Random Element: 98


Here, in the above code, we have a set of integer type [1, 56, 98, 23, 4, 3]. Now we find random element using randomElement() function.

var randEle = myNum.randomElement()!


Hence the random element is 4.

### Example 2

The following program shows how to get a random element from the set.

import Foundation
import Glibc

// Creating a Set of string Type
var myNames: Set = ["Susma", "Punita", "Piku", "Poonam", "Soona"]

// Creating an empty Set
var mySet = Set<String>()

// Getting random elements
// Here we use let to type cast the
// value into the required data type
print("Set 1: ", myNames)
if let setSize1 = myNames.randomElement(){
print("Random Element: ", setSize1)
}
else{
print("Random Number: Nil")
}
print("Set 2: ", mySet)
if let setSize2: String = mySet.randomElement(){
print("Random Element: ", setSize2)
}
else{
print("Random Number: Nil")
}


### Output

Set 1: ["Poonam", "Soona", "Susma", "Piku", "Punita"]
Random Element: Susma
Set 2: []
Random Number: Nil


Here, in the above code, we have two sets: myNames is of String type ["Piku", "Punita", "Poonam", "Soona", “Susma"] and mySet is an empty set. Now we get a random element from the sets using randomElement() −

setSize1 = myNames.randomElement()
let setSize2: String = mySet.randomElement()


Hence the random element from myName set is Susma and nil from mySet because this set is empty.

Updated on: 20-Oct-2022

546 Views