Swift Program to Find Largest Array Element

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to find largest array

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 largest array element from an array, we can use any of the following method −

Iterative
Sorting

Below is a demonstration of the same −

Suppose our given input is −

Myarray = [34, 23, 56, 78, 2]

The desired output would be −

Largest element = 78

Method 1- Using Iterative Method

To find the largest array element we compare all the elements with each other using > operator.

Algorithm

Following is the algorithm −

  • Step 1 − Create a function which takes an array as a parameter.

  • Step 2 − Declare a variable named as maxval to store the largest element.

  • Step 3 − Initially assign maxval with first element to start the comparison.

  • Step 4 − Use for loop to traverse the given array from second element till the last element.

    Compare the current array element with the maxval.

    If the current element is greater than maxval, then replace the value of maxval with current element.

  • Step 5 − Create an array of integer type with value.

  • Step 6 − Call the function and pass the array into it as an argument.

  • Step 7 − Print the output

Example

The following program shows how to find largest array element.

import Foundation import Glibc // Function to find the maximum element func findMaxItem(mArr:[Int]) -> Int{ var maxval = mArr[0] for j in 1..<mArr.count{ if (mArr[j] > maxval){ maxval = mArr[j] } } return maxval } var arr = [34, 4, 54, 6] print("Array:", arr) print("Largest element:", findMaxItem(mArr: arr))

Output

Array: [34, 4, 54, 6]
Largest element: 54

Here, in the above code, we have an array = [34, 4, 54, 6]. Now we pass this array into the function named findMaxItem() to find the largest element of the array. So the working of the above code is −

maxval = mArr[0] = 34

1st iteration −

if (4 > 34) // false{ maxval = mArr[j] }

2nd iteration:

if (54 > 34) // true{ maxval = 54 }

3rd iteration:

if (6 > 54) // false{ maxval = 54 }

Return 54. So the maximum element is 54.

Method 2 - Using Sorting Method

To find the largest element from the array we sort the array in ascending order and then get the last element from the array. Here we use sort() function this will sort array in ascending order.

Syntax

Following is the syntax −

arrayName.sort()

Algorithm

Following is the algorithm −

  • Step 1 − Create a function which takes an array as a parameter.

  • Step 2 − Declare a variable to store the size of the array. Here we find the size of the array using count property.

  • Step 3 − Sort the array using sort() function.

myArray.sort()
  • Step 4 − Return the last element of index size-1.

  • Step 5 − Create an array of integer type with values.

  • Step 6 − Call the function and pass the array into it as an argument.

  • Step 7 − Print the output

Example

The following program shows how to find largest array element.

import Foundation import Glibc // Function to find the maximum element func findMaxItem(mArr:[Int]) -> Int{ var myArray = mArr let size = myArray.count myArray.sort() return myArray[size-1] } var arr = [839, 4, 54, 678] print("Array:", arr) print("Largest element:", findMaxItem(mArr: arr))

Output

Array: [839, 4, 54, 678]
Largest element: 839

Here, in the above code, we have an array = [839, 4, 54, 678]. Now we pass this array into a function named findMaxItem() to find the largest element from the array. So the working of the findMaxItem() function is −

let size = myArray.count = 4
myArray.sort() // Array sorted in ascending order = [4, 54, 678, 839]
return myArray[size-1] = myArray[4-1] = myArray[3] = 839

Hence the output is 839

raja
Updated on 11-Oct-2022 12:14:10

Advertisements