| by Arround The Web | No comments

Swift Array – Min

In this Swift guide, we will see the min() method in the Array Collection.

Before going to discuss this method, we will see what an Array is.

Swift Array

Array in Swift is a collection that stores multiple elements of the same data type.

In Swift, we have to specify the data type to represent the data type of Array while creating an array. It is also possible to create elements in an Array without specifying any data type.

min()

min() in Array Swift can be used to return the minimum element present in the Array. It doesn’t take any parameters.

Syntax:

swift_array.min()

Here, you may need to unwrap the result using “!” symbol. You have to unwrap the result because you will get the result in Optional(result) format.

Unwrapping format – result!

Example 1:

Create a Swift Array that stores ten integer values and finds the minimum element.

//create Swift Array

var swift_array = [12,34,56,2,3,4,78,90,12,34]

// Actual swift array

print("Actual Swift Array: ",swift_array)

//get the minimum value in swift_array

var minimum_element=swift_array.min()

//display the minimum_element

print("Minimum value in swift_array: ", minimum_element!)

Output:

Explanation:

Line -2:

We created an Array named swift_array that holds 10 integer values.

Line -8:

Using the min() function, we are returning only the minimum element among all the elements in the swift_array.

Line -11:

Finally, we are unwrapping the result using “!”.

Example 2:

Create a Swift Array that stores ten double values and finds the minimum element.

//create Swift Array

var swift_array = [12.90,34.87,56.87,2.90,3.32,4.23,7.98,9.90,12.09,390.84]

// Actual swift array

print("Actual Swift Array: ",swift_array)

//get the minimum value in swift_array

var minimum_element=swift_array.min()

//display the minimum_element

print("Minimum element value in swift_array: ", minimum_element!)

Output:

Explanation:

Line -2:

We created an Array named swift_array that holds 10 double values.

Line -8:

Using the min() function, we are returning only the minimum element among all the elements in the swift_array.

Line -11:

Finally, we are unwrapping the result using “!”.

Example3:

Create a Swift Array that stores three strings and finds the minimum element.

//create Swift Array

var swift_array = ["swift1","swift2","linuxhint"]

// Actual swift array

print("Actual Swift Array: ",swift_array)

//get the minimum value in swift_array

var minimum_element=swift_array.min()

//display the minimum_element

print("Minimum value in swift_array: ", minimum_element!)

Output:

Explanation:

Line -2:

We created an Array named swift_array that holds 3 strings.

Line -8:

Now, using the min() function, we are returning only the minimum element among all the elements in the swift_array.

Line -11:

Finally, we are unwrapping the result using “!”.

It is possible to use min() inside the if statement.

Example 4:

Check whether the minimum element in the swift_array is less than 400 or not.

//create Swift Array

var swift_array = [10,20,4,56,70]

// Actual swift array

print("Actual Swift Array: ",swift_array)

//get the minimum value in swift_array

var minimum_element=swift_array.min()

//check the minimum element is less than 10 or not

if (minimum_element! < 10)

{

print("Minimum value in swift_array: ", minimum_element!)
   
}

else{
    print("No element in swift_array is less than 10")
}

Output:

Explanation:

We are checking whether 10 is less than the minimum element in the swift_array. Since the condition is true, it goes inside the if block. Then, the statement inside the if block is executed.

Conclusion

So, we saw how to return the minimum numberof elements from the swift array using the min() method. It doesn’t take any parameters. We may need to unwrap the result using “!” symbol. It is possible to use the min() method with conditional statements using if,if-else etc.

Share Button

Source: linuxhint.com

Leave a Reply