# Swift Program to Find Largest Set Element Using Library Function

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 largest set element using library function.

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.

To find the largest set element Swift provide an in-built function named max(). This function return maximum element from the given set. Here the returned object of type Optional. So we type cast the value into the required datatype. This function will return nil if the given set is empty.

Below is a demonstration of the same −

Suppose our given input is −

MySet = [39, 2, 30, 29, 98, 76, 12]


The desired output would be −

Largest element = 98


## Syntax

Following is the syntax −

var maxItem = mySet.max()


## Algorithm

Following is the algorithm −

• Step 1 − Declare an array with value.

• Step 2 − Find the largest set element using max() function and store the result into a variable.

var maxItem = mySet.max()!


Here using ! We type cast the returned value into Int.

• Step 3 − Print the output

## Example 1

The following program shows how to find the largest set element using library function.

import Foundation
import Glibc

var mySet : Set = [98, 34, 25, 16, 67, 12, 98, 198, 2]
print("Set:", mySet)

// Finding the largest set element
var maxItem = mySet.max()!
print("Maximum Item:", maxItem)


## Output

Set: [198, 34, 16, 12, 25, 67, 2, 98]
Maximum Item: 198


Here, in the above code, we have a set of integer type that is [198, 34, 16, 12, 25, 67, 2, 98]. Now we find the maximum element from the set using max() −

var maxItem = mySet.max()!


Here using ! We type cast the returned value of max() function into Int because it will return value of Optional Type like −

Maximum Item: Optional(198)


So after using type cast the final output is −

Set: [198, 34, 16, 12, 25, 67, 2, 98]
Maximum Item: 198


## Example 2

The following program shows how to find the largest set element using library function.

import Foundation
import Glibc

var mySet = ["gelato", "kulfi", "sherbet", "roll"]
print("Set:", mySet)

// Finding the largest set element
var maxElement = mySet.max()

// Here we unwrap the value using let
switch maxElement{
case .some(let unwrapped):
print("Maximum Element:", unwrapped)
case .none:
print("Nil")
}


## Output

Set: ["gelato", "kulfi", "sherbet", "roll"]
Maximum Element: sherbet


Here, in the above code, we have a set of String type that is ["gelato", "kulfi", "sherbet", "roll"]. Now we find the maximum element from the set using max() −

var maxElement = mySet.max()


Now we unwrap the value using let and the final output is −

Set: ["gelato", "kulfi", "sherbet", "roll"]
Maximum Element: sherbet

Updated on 12-Oct-2022 07:42:32