Data Structures · Visualization
Find Maximum
Array
Finding the maximum works exactly like finding the minimum but flips the comparison: scan every element once while keeping track of the largest value seen so far. Time: O(n). Space: O(1).
Big-O: Random access O(1); insert/delete in the middle O(n).
Find Maximum
ArrayFinding the maximum works exactly like finding the minimum but flips the comparison: scan every element once while keeping track of the largest value seen so far. Every element must be inspected because the biggest value could be anywhere in an unsorted array.
How Find Maximum Works
- Assume the first element is the largest so far.
- Move to the next element and compare it with the current largest.
- If the new element is bigger, update the largest value.
- Repeat until every element has been checked.
- Whatever remains is the maximum.
Find Maximum Worked Example
Input: [7, 2, 9, 1, 5]
- Start: max = 7
- Compare 2 > 7? No → max stays 7
- Compare 9 > 7 → max = 9
- Compare 1 > 9? No → max stays 9
- Compare 5 > 9? No → max stays 9
Output: 9
Press ▶ Run to watch it step by step.
def find_min(arr):
best = arr[0]
for v in arr[1:]:
if v < best:
best = v
return best
def find_max(arr):
best = arr[0]
for v in arr[1:]:
if v > best:
best = v
return best
# example: find maximum of on [7, 2, 9, 1, 5]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Find Maximum Time & Space Complexity
When to Use Find Maximum
- Finding the highest score in a game leaderboard
- Detecting the peak value in sensor readings
- A building block inside sorting and statistics algorithms
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.