Find the Largest Element in an Array
Table of Contents + β
This one looks easy, right? Find the biggest number in a list. But the interviewer is not really checking if you can spot a big number. They want to see if you can walk through an array cleanly, handle the tricky empty case, and pick the approach that does the least work. So let me show you how to talk through it.
π The Problem
You are given an array of numbers. Your job is to return the largest element, which just means the biggest value sitting in that array.
Here is a small example.
Input: [3, 7, 2, 9, 4]Output: 9
Input: [-5, -1, -8, -3]Output: -1See the second one? All numbers are negative. So you cannot just assume the answer starts at zero. A lot of people get this wrong, and we will handle it properly.
π’ The Simple Approach: Sort First
The first idea most people say out loud is this. Sort the array from small to big. Then the last element is the largest. Take it and you are done.
It works. But think about what sorting costs. To sort an array you do a lot of comparing and moving around, and that takes O(n log n) time. Here n is the number of elements in the array. That is more work than the problem needs, because you are arranging every single element just to read one value at the end.
So this is fine as a first answer. But the interviewer will then ask, βCan you do better?β
π The Better Approach: One Single Pass
Here is the trick. You do not need the whole array sorted. You only need the biggest one. So just walk through the array once and remember the biggest value you have seen so far.
Start by guessing that the first element is the largest. Then look at each next element. If it is bigger than your current guess, update your guess. When you reach the end, your guess is the real answer. We call this a single pass, because you touch every element only one time.
This runs in O(n) time. You look at each element once, and that is it.
Steps to find the largest element
- First check if the array is empty. If it is, there is no largest element, so report that clearly.
- Set a variable
maxto the first element of the array. - Go through the rest of the elements one by one.
- If the current element is greater than
max, setmaxto that element. - After the loop ends,
maxholds the largest value. Return it.
Now let me show the single-pass version in all five languages.
# Returns the largest value in arr.# Returns None when the array is empty.def find_largest(arr): if not arr: # handle the empty case first return None max_value = arr[0] # start with the first element for num in arr[1:]: # look at every other element if num > max_value: # found a bigger one max_value = num return max_value
arr = [3, 7, 2, 9, 4]result = find_largest(arr)
if result is None: print("Array is empty, no largest element.")else: print("Largest element:", result)The output of the above code will be:
Largest element: 9Notice how every version starts max at the first element, not at zero. That is what keeps the all-negative case correct. If you start at zero, then for [-5, -1, -8, -3] your code would wrongly say zero, because no negative number ever beats zero.
β±οΈ Time and Space Complexity
Here is how the two approaches stack up.
| Approach | Time | Space |
|---|---|---|
| Sort, then take the last element | O(n log n) | O(1) to O(n) |
| Single pass with a max variable | O(n) | O(1) |
The single pass wins on time, and it uses only one extra variable. So it is the answer you want to give.
Tip
Most languages ship a built-in for this. Python has max(arr), C++ has *max_element(arr.begin(), arr.end()), and JavaScript has Math.max(...arr). In a real job you would use these. But in an interview, write the loop yourself first to show you understand it. Then mention the built-in as the clean production version.
Caution
Always ask the interviewer what should happen for an empty array. Some want null or None, some want an error, some say it will never be empty. Saying this out loud shows you think about edge cases before you code.
π§© Key Takeaways
- β You only need a single pass in O(n) time. Sorting first is O(n log n), so it does extra work you do not need.
- β
Start your
maxat the first element, never at zero, so arrays of all negative numbers still work. - β Handle the empty array up front and agree with the interviewer on what to return.
- β Know the built-in shortcut, but be ready to write the loop by hand to prove you understand it.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
Why is the single-pass approach better than sorting to find the largest element?
Why: Sorting arranges every element at O(n log n) cost, but you only need one walk through the array, which is O(n).
- 2
Why do we start the max variable at the first element instead of zero?
Why: If you start at zero, an all-negative array like [-5, -1, -8] would wrongly return zero since no negative beats zero.
- 3
What should you do before writing code for an empty array?
Why: Different teams want different behavior, so confirming the empty-array rule shows you think about edge cases first.
- 4
What is the space complexity of the single-pass approach?
Why: You only keep one extra variable for the max, so the extra space stays constant no matter how big the array is.