The largest element in an array is the element that has the maximum value. For example if an array has the element set {20, 30, 10, 40, 70, 100}, then the largest element is 100. We have to check each element for an unsorted array to find the largest element.

## 1. Algorithm to find the largest element in an array

1. Take input of an array A[] and the size n

2. Initially set the first element as maximum element i.e. max = A[0]

3. Set i = 0

4. Check if max < A[i]

5. If step 4 is true set max = A[i] and i = i + 1.

6. If step 4 is false set i = i + 1.

7. If i < n, go to step 4 else display max as the largest element.

## 2. Pseudocode to find the largest element in an array

Input: An array $A[~]$ and size of array $n$

Output: Largest element in $A[~]$

1. Procedure largestInArray($A[~]$, $n$):

2. $max \leftarrow A[0]$

3. Repeat for $i \in [0, n - 1]$:

4. If $max < A[i]$:

5. $max = A[i]$

6. Return $max$

7. End Procedure

## 3. Time complexity to find the largest element in an array

Time Complexity: O(n)

Where n is the total no of elements in the unsorted array.

## 4. Find the largest element in an array using Iteration

In this section, the iterative programs to find the largest element in an array are given.

## 4.1. C Program & output to find the largest element in an array using Iteration

Code has been copied
/********************************
alphabetacoder.com
C program to find largest element
in an array using iteration
*********************************/

#include <stdio.h>

int main() {
// declare an array
int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int i, max;

// set the first element as the max element
max = arr[0];

// compare each element in array
// to find the largest element
for (i = 0; i < 8; i++) {
if (max < arr[i])
max = arr[i];
}

// display result
printf("%d is the largest element!", max);

return 0;
}


Output

50 is the largest element!

## 4.2. C++ Program & output to find the largest element in an array using Iteration

Code has been copied
/**********************************
alphabetacoder.com
C++ program to find largest element
in an array using iteration
***********************************/

#include <iostream>

using namespace std;

int main() {
// declare an array
int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int i, max;

// set the first element as the max element
max = arr[0];

// compare each element in array
// to find the largest element
for (i = 0; i < 8; i++) {
if (max < arr[i])
max = arr[i];
}

// display result
cout << max << " is the largest element!";

return 0;
}


Output

50 is the largest element!

## 4.3. Java Program & output to find the largest element in an array using Iteration

Code has been copied
/***********************************
alphabetacoder.com
Java program to find largest element
in an array using iteration
************************************/

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
// declare an array
int arr[] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int i, max;

// set the first element as the max element
max = arr[0];

// compare each element in array
// to find the largest element
for (i = 0; i < arr.length; i++) {
if (max < arr[i])
max = arr[i];
}

// display result
System.out.println(max + " is the largest element!");
}
}


Output

50 is the largest element!

## 4.4. Python Program & output to find the largest element in an array using Iteration

Code has been copied
# ************************************
#        alphabetacoder.com
# Python program to find largest
# element in an array using iteration
# ************************************

# declare a list
arr = [2, 50, 7, 23, 12, 17, 9, 10]

# set the first element as the max element
maximum = arr[0]

# compare each element in array
# to find the largest element
for i in range(len(arr)):
if maximum < arr[i]:
maximum = arr[i]

# display result
print(maximum, "is the largest element!")


Output

50 is the largest element!

## 4.5. C# Program & output to find the largest element in an array using Iteration

Code has been copied
/*********************************
alphabetacoder.com
C# program to find largest element
in an array using iteration
**********************************/
using System;

namespace LargestElement {
class Program {
static void Main(string[] args) {
// declare an array
int[] arr = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int i, max;

// set the first element as the max element
max = arr[0];

// compare each element in array
// to find the largest element
for (i = 0; i < 8; i++) {
if (max < arr[i])
max = arr[i];
}

// display output
Console.WriteLine(max + " is the largest element!");

// wait for user to press any key
}
}
}


Output

50 is the largest element!

## 5. Find the largest element in an array using recursion

In this section, the recursive programs to find the largest element in an array are given.

## 5.1. C Program & output to find the largest element in an array using recursion

Code has been copied
/********************************
alphabetacoder.com
C program to find largest element
in an array using recursion
*********************************/

#include <stdio.h>

// recursive function to find
// largest element in array
int find_largest(int arr[], int largest, int i){
// exit condition
if(i < 0)
return largest;
// compare largest with
// current element
if(largest <= arr[i])
largest = arr[i];
// call function
return find_largest(arr, largest, i - 1);
}

int main() {
// declare an array
int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int max, length;

// calculate size of array
length = sizeof(arr)/sizeof(arr[0]);

// call function to get the
// largest element as return value
// pass the array, first element as
// default largest element and the
// size of the array
max = find_largest(arr, arr[0], length - 1);

// display result
printf("%d is the largest element!", max);

return 0;
}


Output

50 is the largest element!

## 5.2. C++ Program & output to find the largest element in an array using recursion

Code has been copied
/**********************************
alphabetacoder.com
C++ program to find largest element
in an array using recursion
***********************************/

#include <iostream>

using namespace std;

// recursive function to find
// largest element in array
int find_largest(int arr[], int largest, int i) {
// exit condition
if (i < 0)
return largest;
// compare largest with
// current element
if (largest <= arr[i])
largest = arr[i];
// call function
return find_largest(arr, largest, i - 1);
}

int main() {
// declare an array
int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int max, length;

// calculate size of array
length = sizeof(arr) / sizeof(arr[0]);

// call function to get the
// largest element as return value
// pass the array, first element as
// default largest element and the
// size of the array
max = find_largest(arr, arr[0], length - 1);

// display result
cout << max << " is the largest element!";

return 0;
}


Output

50 is the largest element!

## 5.3. Java Program & output to find the largest element in an array using recursion

Code has been copied
/***********************************
alphabetacoder.com
Java program to find largest element
in an array using recursion
************************************/

import java.util.Scanner;

public class Main {

// recursive function to find
// largest element in array
public static int find_largest(int arr[], int largest, int i) {
// exit condition
if (i == arr.length)
return largest;
// compare largest with
// current element
if (largest <= arr[i])
largest = arr[i];
// call function
return find_largest(arr, largest, i + 1);
}

public static void main(String[] args) {
// declare an array
int arr[] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int max;

// call function to get the
// largest element as return value
// pass the array, first element as
// default largest element and the
// size of the array
max = find_largest(arr, arr[0], 0);

// display result
System.out.println(max + " is the largest element!");
}
}


Output

50 is the largest element!

## 5.4. Python Program & output to find the largest element in an array using recursion

Code has been copied
# ************************************
#        alphabetacoder.com
# Python program to find largest
# element in an array using recursion
# ************************************

# recursive function
def find_largest(arr, largest, i):
# exit condition
if i == len(arr):
return largest

# compare the largest with
# the current element
if largest <= arr[i]:
largest = arr[i]

# call function
return find_largest(arr, largest, (i + 1))

def main():
# declare a list
arr = [2, 50, 7, 23, 12, 17, 9, 10]

# call function to get the
# largest element as return value
maximum = find_largest(arr, arr[0], 0)

# display result
print(maximum, "is the largest element!")

# call main() function
main()


Output

50 is the largest element!

## 5.5. C# Program & output to find the largest element in an array using recursion

Code has been copied
/*********************************
alphabetacoder.com
C# program to find largest element
in an array using recursion
**********************************/
using System;

namespace LargestElement {
class Program {
// recursive function to find
// largest element in array
public static int find_largest(int[] arr, int largest, int i) {
// exit condition
if (i == arr.Length)
return largest;
// compare largest with
// current element
if (largest <= arr[i])
largest = arr[i];
// call function
return find_largest(arr, largest, i + 1);
}

static void Main(string[] args) {
// declare an array
int[] arr = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int max = 0;

// call function to get the max element
max = find_largest(arr, arr[0], 0);

// display output
Console.WriteLine(max + " is the largest element!");

// wait for user to press any key
}
}
}


Output

50 is the largest element!

## 6. Find the largest element in an array using function

In this section, the programs using the inbuilt functions to find the largest element in an array are given.

## 6.1. C++ Program & output to find the largest element in an array using STL function max_element()

Code has been copied
/**************************************
alphabetacoder.com
C++ program to find the largest element
in an array using STL function
***************************************/

#include <iostream>

#include<algorithm>

using namespace std;

int main() {
// declare an array
int arr[8] = {2, 50, 7, 23, 12, 17, 9, 10};
// declare variables
int max;

// get the maximum element using function
max = * max_element(arr, arr + 8);

// display result
cout << max << " is the largest element!";

return 0;
}


Output

50 is the largest element!

## 6.2. Python Program & output to find the largest element in an array using max() function

Code has been copied
# ***************************************
#        alphabetacoder.com
# Python program to find the largest
# element in a list using max() function
# ***************************************

# declare a list
arr = [2, 50, 7, 23, 12, 17, 9, 10]

# get the maximum element using function
maximum = max(arr)

# display result
print(maximum, "is the largest element!")


Output

50 is the largest element!