Find Maximum Value of an Array in Python

Beginner
⏱️ 9 min read
📚 Updated: May 2026
🎯 2 Code Examples
Lists

What you’ll learn

  • The classic linear scan with a running maximum.
  • How to write a reusable find_max helper in Python.
  • A second all-negative example and a live preview.

Overview

One left-to-right pass is enough to find the largest number in an unsorted list.

Two examples

Mixed positive list and all-negative list.

Live preview

Shows max for the same sample data as code example 1.

Interview focus

Explain O(n) scan and empty-list handling.

Prerequisites

Basic loops and lists in Python.

  • Understand list indexing and for loops.
  • Know that Python lists can contain negative and positive integers.

The idea

Start with the first element as current maximum. Whenever you find a larger number, update the maximum.

After one pass, the current maximum is the answer.

Live preview

Uses the same sample list as example 1: 14, 7, 25, 31, 10, 42.

Runs the same linear-scan logic in JavaScript.

Live result
Press "Find maximum".

Algorithm

Goal: return the largest value in a list of n integers.

Handle empty list

If list is empty, raise an error or return a special value.

Initialize maximum

Set max_val = arr[0].

Scan remaining values

For each next element, update max_val if current element is larger.

Return answer

Return final max_val.

📜 Pseudocode

Pseudocode
function find_max(arr):
    if arr is empty:
        error
    max_val <- arr[0]
    for each value in arr from second element:
        if value > max_val:
            max_val <- value
    return max_val
1

Find maximum (reference program)

A reusable helper function and a sample list with answer 42.

python
def find_max(arr: list[int]) -> int:
    if not arr:
        raise ValueError("Array must contain at least one element.")

    max_val = arr[0]
    for i in range(1, len(arr)):
        if arr[i] > max_val:
            max_val = arr[i]
    return max_val

def main() -> None:
    array = [14, 7, 25, 31, 10, 42]
    max_value = find_max(array)
    print(f"Maximum value in the array: {max_value}")

if __name__ == "__main__":
    main()

Explanation

The function raises a clear error for empty input and uses one simple pass for normal lists.

2

When every element is negative

The same algorithm still works; maximum is the least negative value.

python
def find_max(arr: list[int]) -> int:
    if not arr:
        raise ValueError("Array must contain at least one element.")
    max_val = arr[0]
    for x in arr[1:]:
        if x > max_val:
            max_val = x
    return max_val

def main() -> None:
    negatives = [-9, -3, -1, -7]
    print(f"Maximum (least negative): {find_max(negatives)}")

if __name__ == "__main__":
    main()

Notes

Built-in option. Python has max(arr), but interviewers often ask you to write the manual scan first.

Huge data. Parallel scans can combine partial maxima in large-scale systems.

❓ FAQ

It picks the first element as the initial candidate, then every later element gets compared against it.
There is no maximum value for an empty list. Handle this case explicitly with an error or validation.
Yes. The maximum is still the greatest value, even if it is negative (for example -1 is greater than -3).
The algorithm still returns that maximum value. Duplicates do not change the answer.
No. Sorting is usually slower than a single scan when you only need the maximum.
One pass over n elements: O(n) time and O(1) extra space.

🔄 Input / output

You can read integers from input, store them in a list, and pass that list to find_max.

Edge cases

Empty

Empty list

No maximum exists; handle this before scanning.

Single

One element

That element is automatically the maximum.

⏱️ Time and space complexity

ApproachTimeExtra space
Single scanO(n)O(1)

Summary

  • Algorithm: running maximum in one pass.
  • Cost: O(n) time, O(1) extra space.
  • Important edge: define behavior for empty list.
Did you know?

Finding the maximum in an unsorted list needs at least n - 1 comparisons in the worst case. A single left-to-right scan gives O(n) time and O(1) extra space.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful