- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Swift Program to Find Minimum Array Element Using Library Function

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

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).

- Related Questions & Answers
- C++ Program to Find Minimum Element in an Array using Linear Search
- PHP program to find the minimum element in an array
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- Program to find minimum numbers of function calls to make target array using Python
- C# program to find maximum and minimum element in an array
- Program to find minimum distance to the target element using Python
- Program to find minimum operations to make array equal using Python
- Program to find the minimum (or maximum) element of an array in C++
- How to remove Specific Element from a Swift Array?
- Program to find minimum operations to make the array increasing using Python
- Write a C program using time.h library function
- How to find minimum element in an array using linear search in C language?
- How to find minimum element in an array using binary search in C language?
- Write a Golang program to find the element with the minimum value in an array
- How to find the minimum and maximum element of an Array using STL in C++?
- C++ Program to Find Maximum Element in an Array using Binary Search