| by Arround The Web | No comments

Swift Array – Max

In this Swift guide, we will see the max() 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 can also be possible to create elements in an Array without specifying any data type.

max()

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

Syntax:

swift_array.max()

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 maximum 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 maximum value in swift_array

var maximum_element=swift_array.max()

//display the maximum_element

print("Maximum value in swift_array: ", maximum_element!)

Output:

Explanation:

Line -2:

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

Line -8:

Using the max() function, we are returning only the maximum 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 maximum 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 maximum value in swift_array

var maximum_element=swift_array.max()

//display the maximum_element

print("Maximum value in swift_array: ", maximum_element!)

Output:

Explanation:

Line -2:

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

Line -8:

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

Line -11:

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

Example 3:

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

//create Swift Array

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

// Actual swift array

print("Actual Swift Array: ",swift_array)

//get the maximum value in swift_array

var maximum_element=swift_array.max()

//display the maximum_element

print("Maximum value in swift_array: ", maximum_element!)

Output:

Explanation:

Line -2:

We created an Array named swift_array that holds 3 strings.

Line -8:

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

Line -11:

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

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

Example 4:

So, check whether the maximum element in the swift_array is greater 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 maximum value in swift_array

var maximum_element=swift_array.max()

//check the maximum element is greater than 400 or not

if (maximum_element! > 400)

{

print("Maximum value in swift_array: ", maximum_element!)

 

}

else{

print("No element in swift_array is greater than 400")

}

Output:

Explanation:

We are checking whether 400 is greater than the maximum element in the swift_array. Since the condition fails inside the if block, then the statement – “No element in swift_array is greater than 400” is returned from the else block.

Conclusion

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

Share Button

Source: linuxhint.com

Leave a Reply