Swift Program to Find Minimum Array 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 minimum array element using library function.

An array is an ordered collection which is used to store same type of data. For example, if any array is of integer type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc.

To find the smallest array element Swift provide an in-built function named min(). This function return minimum element from the given array. It will return nil if the given array is empty. 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 array is empty.

In this function, we can also provide a predicate, due to which a comparison takes place between the elements.

Below is a demonstration of the same −

Suppose our given input is −

Myarray = [34, 23, 56, 78, 2, 67, 834, 45, 665]


The desired output would be −

Smallest element = 2


Syntax

Following is the syntax −

Arr.min()


Algorithm

Following is the algorithm −

• Step 1 − Declare an array with value.

• Step 2 − Find the minimum array element using min() function.

var minElement = myarray.min()!


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 minimum array element using library function.

import Foundation
import Glibc

var myarray = [34, 5, 59, 2, 494, 4, 392, 58, 23]
print("Array:", myarray)

// Finding the smallest array element
var minElement = myarray.min()!

print("Smallest element: ", minElement)


Output

Array: [34, 5, 59, 2, 494, 4, 392, 58, 23]
Smallest element: 2


Here, in the above code, we have an array = [34, 5, 59, 2, 494, 4, 392, 58, 23]. Now we find the minimum element from the array using min() −

var minElement = myarray.min()!


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

Array: [34, 5, 59, 2, 494, 4, 392, 58, 23]
Smallest element: Optional(2)


So after using type cast the final output is −

Array: [34, 5, 59, 2, 494, 4, 392, 58, 23]
Smallest element: 2


Example 2

The following program shows how to find the minimum array element using library function. import Foundation

import Glibc
let myArr = ["cat", "donkey", "monkey", "owl", "bird"]
print("Array:", myArr)

// Finding the smallest array element
// Whose width is also the smallest
// among all the elements
if let output = myArr.min(by: {x, y in x.count < y.count}) {
print("Minimum element : \(output)")
} else{
print("Array is empty")
}


Output

Array: ["cat", "donkey", "monkey", "owl", "bird"]
Minimum element : cat


Here, in the above code, we have an array = ["cat", "donkey", "monkey", "owl", "bird"]. Now we find the minimum element from the array using min(), here the length of the element should also small among all the given elements so we use predicate in the min() function −

let output = myArr.min(by: {x, y in x.count < y.count})


It compare the length of the elements with each other. So we get cat as the minimum element because cat is the minimum element(as compare to owl), also the length of the cat is minimum from the rest of the given elements(except owl).

Updated on 11-Oct-2022 12:03:28