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

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. Every element must be inspected because the biggest value could be anywhere in an unsorted array.

How Find Maximum Works

  1. Assume the first element is the largest so far.
  2. Move to the next element and compare it with the current largest.
  3. If the new element is bigger, update the largest value.
  4. Repeat until every element has been checked.
  5. Whatever remains is the maximum.

Find Maximum Worked Example

Input: [7, 2, 9, 1, 5]

  1. Start: max = 7
  2. Compare 2 > 7? No → max stays 7
  3. Compare 9 > 7 → max = 9
  4. Compare 1 > 9? No → max stays 9
  5. Compare 5 > 9? No → max stays 9

Output: 9

Try your own input
Find MaximumO(n)
Scans every element once, keeping track of the largest value seen so far.
7
0
2
1
9
2
1
3
5
4
Active / shifting Result
Find Maximum
Press ▶ Run to watch it step by step.
Python · Arrays in Code
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]
Input: [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

TimeO(n)
SpaceO(1)

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.