Find the Largest Element in an Array Using Python
Four Python methods to find the largest element in an array: loop, max(), sorted(), and NumPy. Includes time complexity notes for campus placement coding rounds.
Array-maximum questions appear in nearly every campus coding test, and Python gives you at least four ways to solve them, each with a different trade-off between code length and time complexity.
This article covers all four. If you’re preparing for a CRT session or a first-round placement test, the loop method and max() are the two you need to know cold. The others are good to recognise when they come up.
What this problem tests in a placement round
The problem statement is always simple: given a list like [10, 20, 4, 45, 99], return 99. What the examiner evaluates varies by round type:
- In a timed online assessment, correct output quickly is the only goal.
- In a face-to-face round, the interviewer may ask you to write the loop by hand and explain each comparison step.
- In a data-science or analytics role interview, the follow-up is typically about what changes when the array has millions of elements.
Knowing all four approaches, and their time and space trade-offs, covers all three scenarios without memorising different solutions for each.
The examples below all use the same array:
arr = [10, 20, 4, 45, 99]
Method 1: Linear scan with a loop
The manual approach is the most instructive. It requires no imports and runs in O(n) time with O(1) extra space.
def find_largest(arr):
largest = arr[0] # Start with the first element as the candidate
for num in arr:
if num > largest:
largest = num # Update whenever a bigger value is found
return largest
arr = [10, 20, 4, 45, 99]
print(find_largest(arr)) # Output: 99
How it works, step by step
- Initialize
largesttoarr[0], which is10. - Compare
10with20:20is larger, so updatelargest = 20. - Compare
20with4: no update. - Compare
20with45: updatelargest = 45. - Compare
45with99: updatelargest = 99. - Loop ends. Return
99.
One guard worth adding in production code: if not arr: return None before the arr[0] line, so an empty list raises a clear None rather than an IndexError.
Time complexity: O(n). Space complexity: O(1). Write this version when the problem statement says “implement the logic without using built-in functions.”
For a related comparison problem on exactly three candidates, see finding the greatest of three numbers. The same update-and-compare pattern extends naturally.
Method 2: Built-in max()
Python’s max() function finds the maximum value in a list with no import required.
arr = [10, 20, 4, 45, 99]
print(max(arr)) # Output: 99
Internally, max() runs the same linear scan as the loop above. Time complexity is still O(n). The implementation is in C rather than interpreted Python, which makes it faster in practice, but the asymptotic class does not change.
Use max() in any timed test where writing the full loop is unnecessary. One line, no imports, correct output.
Method 3: sorted() and when to avoid it
Python’s sorted() function returns a new list in ascending order. The last element is the maximum.
arr = [10, 20, 4, 45, 99]
sorted_arr = sorted(arr)
print(sorted_arr[-1]) # Output: 99
When sorted() makes sense
- You already need the sorted list for a follow-up operation (finding median, rank, or distribution).
- The problem statement explicitly asks for a sorting-based solution.
When to avoid it
If you only need the maximum value, sorted()[-1] costs O(n log n) while max() costs O(n). An interviewer who asks “can this be more efficient?” is flagging exactly this trade-off. The extra cost of sorting comes from Timsort’s comparisons, none of which contribute to finding the max.
Method 4: NumPy for numerical arrays
NumPy is the standard library for numerical computing in Python. Its np.max() scans a NumPy array and returns the maximum.
import numpy as np
arr = np.array([10, 20, 4, 45, 99])
print(np.max(arr)) # Output: 99
Time complexity is O(n); NumPy still examines every element. The advantage is vectorized execution: the inner loop runs in compiled C rather than interpreted Python, which matters when the array has millions of elements rather than five.
For a placement coding test, this is overkill unless the problem explicitly says “use NumPy.” For a data-science or ML interview where the dataset is large, it’s the natural choice.
Side-by-side comparison
| Method | Import needed | Time complexity | Space complexity | Best when |
|---|---|---|---|---|
| Loop | None | O(n) | O(1) | Test asks for explicit logic |
| max() | None | O(n) | O(1) | Any timed test, general case |
| sorted()[-1] | None | O(n log n) | O(n) | Sorted output needed anyway |
| np.max() | numpy | O(n) | O(1) | Large numerical datasets |
Connecting this to broader array work
Finding the maximum is one operation in a family of related array problems. The linear-scan pattern from Method 1 is the same structure that appears in sum of array elements in Python. Swap if num > largest for a running total and the skeleton is identical. More foundational Python programs, including string and character operations, are collected in Python practice programs.
Choosing the right method for your test
For a 30-minute campus placement round: write max(arr) unless the problem explicitly forbids built-ins. For a manual-implementation question: write the loop and walk through each comparison step. For a data-science or analytics interview: mention NumPy and explain that the O(n) complexity stays the same but the constant factor drops because the loop runs in compiled C.
Knowing max() solves the problem in one line is useful. Knowing why sorted()[-1] costs O(n log n) while max() costs O(n) is what interviewers probe in the follow-up. To apply that kind of reasoning to real data (ranking model outputs, finding the highest-confidence prediction from a classifier’s array, parsing JSON responses from an API), TinkerLLM provides a Python notebook environment with LLM APIs already wired in, at ₹299.
Primary sources
Frequently asked questions
What is the time complexity of finding the largest element in a Python list?
The loop method and max() are both O(n) — they scan the list once. sorted()[-1] is O(n log n) because it sorts first. NumPy's np.max() is also O(n) but with a lower constant due to vectorized C code.
Does max() work directly on a Python list without any import?
Yes. max() is a built-in function — no import needed. You can call max([10, 20, 4, 45, 99]) directly and it returns 99.
What happens if the array is empty when using the loop method?
The line largest = arr[0] raises an IndexError if arr is empty. Add a guard: if not arr: return None, before accessing arr[0]. The built-in max() raises a ValueError on an empty list by default.
Which method should I use in a campus placement coding test?
Use max() for speed of writing. If the question says to implement the logic manually, use the loop method — it shows the algorithm clearly and is easy to walk through step by step.
Can I find the largest element in a 2D array (matrix) using the same methods?
For a 2D Python list, max(max(row) for row in matrix) works in one line. With NumPy, np.max(matrix) finds the global maximum across all rows and columns.
A self-paced playground for building with LLMs.
TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.
Try TinkerLLM (₹299 launch)