problemeasyalgorithmsfind-the-max-element-in-arrayfind the max element in arrayfindthemaxelementinarray

Find Largest element in array

EasyUpdated: Sep 1, 2025

Problem

Given an array arr of size N, find the largest (maximum) element in the array.

Examples

Example 1

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

Example 2

Input: arr = [-5, 0, 12, 8, -2]
Output: 12
Explanation: The largest value is 12.

Solution

Method 1 – Sorting

Intuition

Sorting the array puts the largest element at the end. The last element after sorting is the maximum.

Approach

  1. Sort the array in ascending order.
  2. Return the last element as the answer.

Code

C++
class Solution {
public:
  int largestElement(vector<int>& arr) {
    sort(arr.begin(), arr.end());
    return arr.back();
  }
};
Go
func largestElement(arr []int) int {
  sort.Ints(arr)
  return arr[len(arr)-1]
}
Java
class Solution {
  public int largestElement(int[] arr) {
    Arrays.sort(arr);
    return arr[arr.length - 1];
  }
}
Kotlin
class Solution {
  fun largestElement(arr: IntArray): Int {
    arr.sort()
    return arr.last()
  }
}
Python
class Solution:
  def largestElement(self, arr: list[int]) -> int:
    arr.sort()
    return arr[-1]
Rust
impl Solution {
  pub fn largest_element(arr: &mut [i32]) -> i32 {
    arr.sort();
    *arr.last().unwrap()
  }
}

Complexity

  • ⏰ Time complexity: O(n log n), due to sorting the array of size n.
  • 🧺 Space complexity: O(1) if sorting in-place, otherwise O(n) if a new array is created.

Method 2 – Linear Scan

Intuition

The largest element can be found by scanning the array and keeping track of the maximum seen so far.

Approach

  1. Initialize a variable ans with the first element of the array.
  2. Iterate through the array:
  • If the current element is greater than ans, update ans.
  1. Return ans after the loop.

Code

C++
class Solution {
public:
  int largestElement(vector<int>& arr) {
    int ans = arr[0];
    for (int i = 1; i < arr.size(); ++i) {
      if (arr[i] > ans) ans = arr[i];
    }
    return ans;
  }
};
Go
func largestElement(arr []int) int {
  ans := arr[0]
  for _, v := range arr[1:] {
    if v > ans {
      ans = v
    }
  }
  return ans
}
Java
class Solution {
  public int largestElement(int[] arr) {
    int ans = arr[0];
    for (int i = 1; i < arr.length; i++) {
      if (arr[i] > ans) ans = arr[i];
    }
    return ans;
  }
}
Kotlin
class Solution {
  fun largestElement(arr: IntArray): Int {
    var ans = arr[0]
    for (i in 1 until arr.size) {
      if (arr[i] > ans) ans = arr[i]
    }
    return ans
  }
}
Python
class Solution:
  def largestElement(self, arr: list[int]) -> int:
    ans = arr[0]
    for x in arr[1:]:
      if x > ans:
        ans = x
    return ans
Rust
impl Solution {
  pub fn largest_element(arr: &[i32]) -> i32 {
    let mut ans = arr[0];
    for &x in &arr[1..] {
      if x > ans { ans = x; }
    }
    ans
  }
}

Complexity

  • ⏰ Time complexity: O(n), since each element is checked once.
  • 🧺 Space complexity: O(1), only a constant amount of extra space is used.

Continue Practicing

Comments