Iterative Approach to find the largest element of Array

The simplest approach is to solve this problem is to traverse the whole list and find the maximum among them.

  • Create a local variable max and initiate it to arr[0] to store the maximum among the list
  • Iterate over the array
    • Compare arr[i] with max.
    • If arr[i] > max, update max = arr[i].
    • Increment i once.
  • After the iteration is over, return max as the required answer.

Below is the implementation of the above approach: 

C++




// C++ program to find maximum in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum of array
int largest(int arr[], int n)
{
    int i;
 
    // Initialize maximum element
    int max = arr[0];
 
    // Traverse array elements
    // from second and compare
    // every element with current max
    for (i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];
 
    return max;
}
 
// Driver Code
int main()
{
    int arr[] = { 10, 324, 45, 90, 9808 };
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    cout << "Largest in given array is " << largest(arr, n);
    return 0;
}
 
// This Code is contributed by Shivi_Aggarwal


C




// C program to find maximum in arr[] of size n
#include <stdio.h>
 
// Function to find maximum in arr[] of size n
int largest(int arr[], int n)
{
    int i;
 
    // Initialize maximum element
    int max = arr[0];
 
    // Traverse array elements from second and
    // compare every element with current max
    for (i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];
 
    return max;
}
 
// Driver code
int main()
{
    int arr[] = { 10, 324, 45, 90, 9808 };
    int n = sizeof(arr) / sizeof(arr[0]);
   
    // Function call
    printf("Largest in given array is %d", largest(arr, n));
    return 0;
}


Java




// Java Program to find maximum in arr[]
import java.io.*;
 
class Test {
    static int arr[] = { 10, 324, 45, 90, 9808 };
 
    // Method to find maximum in arr[]
    static int largest()
    {
        int i;
 
        // Initialize maximum element
        int max = arr[0];
 
        // Traverse array elements from second and
        // compare every element with current max
        for (i = 1; i < arr.length; i++)
            if (arr[i] > max)
                max = arr[i];
 
        return max;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        System.out.println("Largest in given array is "
                           + largest());
    }
}


Python3




# Python3 program to find maximum in arr[] of size n
 
 
# Function to find maximum in arr[] of size n
def largest(arr, n):
     
    # Initialize maximum element
    mx = arr[0]
 
    # Traverse array elements from second
    # and compare every element with
    # current max
    for i in range(1, n):
        if arr[i] > mx:
            mx = arr[i]
 
    return mx
 
 
# Driver Code
if __name__ == '__main__':
    arr = [10, 324, 45, 90, 9808]
    n = len(arr)
     
    # Calculating length of an array
    Ans = largest(arr, n)
     
    # display max
    print("Largest in given array is", Ans)
 
 
# This code is contributed by Jai Parkash Bhardwaj
# and improved by prophet1999


C#




// C# Program to find maximum in arr[]
using System;
 
class GFG {
 
    static int[] arr = { 10, 324, 45, 90, 9808 };
 
    // Method to find maximum in arr[]
    static int largest()
    {
        int i;
 
        // Initialize maximum element
        int max = arr[0];
 
        // Traverse array elements from second and
        // compare every element with current max
        for (i = 1; i < arr.Length; i++)
            if (arr[i] > max)
                max = arr[i];
 
        return max;
    }
 
    // Driver code
    public static void Main()
    {
        Console.WriteLine("Largest in given "
                          + "array is " + largest());
    }
}
 
// This code is contributed by anuj_67.


Javascript




<script>
 
// JavaScript program to find
// maximum in arr[] of size n
   
    function largest(arr) {
        let i;
       
        // Initialize maximum element
        let max = arr[0];
   
        // Traverse array elements 
        // from second and compare
        // every element with current max 
        for (i = 1; i < arr.length; i++) {
            if (arr[i] > max)
                max = arr[i];
        }
         
        return max;
    }
     
    // Driver code
    let arr = [10, 324, 45, 90, 9808];
    document.write("Largest in given array is " + largest(arr));
     
 // This code is contributed by Surbhi Tyagi 
  
</script>


PHP




<?php
// PHP program to find maximum
// in arr[] of size n
 
// PHP function to find maximum
// in arr[] of size n
function largest($arr, $n)
{
    $i;
     
    // Initialize maximum element
    $max = $arr[0];
 
    // Traverse array elements
    // from second and
    // compare every element
    // with current max
    for ($i = 1; $i < $n; $i++)
        if ($arr[$i] > $max)
            $max = $arr[$i];
 
    return $max;
}  
     
// Driver Code
$arr= array(10, 324, 45, 90, 9808);
$n = sizeof($arr);
echo "Largest in given array is "
                 , largest($arr, $n);
     
// This code is contributed by aj_36
?>


Output

Largest in given array is 9808

Time complexity: O(N), to traverse the Array completely.
Auxiliary Space: O(1), as only an extra variable is created, which will take O(1) space.

Program to find largest element in an Array

Given an array arr[] of size N, the task is to find the largest element in the given array. 

Examples: 

Input: arr[] = {10, 20, 4}
Output: 20
Explanation: Among 10, 20 and 4, 20 is the largest. 

Input : arr[] = {20, 10, 20, 4, 100}
Output : 100

Recommended Practice

Similar Reads

Iterative Approach to find the largest element of Array:

The simplest approach is to solve this problem is to traverse the whole list and find the maximum among them....

Recursive Approach to find the maximum of Array:

...

Find the maximum of Array using Library Function:

...

Contact Us