Find Maximum Value of an Array in C#

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
1D arrays

What you’ll learn

  • How a linear scan keeps a running maximum while you walk the array once.
  • How to write a reusable FindMax method with arr.Length and a for loop.
  • Why the same logic works for negatives and duplicates, and what to do for an empty array.

Overview

Finding the maximum is one of the first array exercises in C#. You do not need to sort — just remember the biggest value seen so far and update it whenever a larger element appears.

One pass

O(n) scan — no sorting required.

Live preview

Sample array from example 1 — result 42.

Real uses

Scores, sensor peaks, price highs — same loop pattern.

Prerequisites

int[] arrays, for loops, comparisons, and Console.WriteLine.

  • You can iterate with for (int i = 0; i < arr.Length; i++) and read arr[i].
  • You understand that maximum means the greatest numeric value in the list.

The idea

Keep one current winner (max). Scan left to right and replace it whenever you see a bigger value.

One pass is enough — you never need to look ahead or sort. After visiting every element, max holds the answer.

Start max = arr[0]
Update if arr[i] > max
Loop i = 1 .. Length-1

Walk-through

For {14, 7, 25, 31, 10, 42}: start with max = 14. After seeing 25 then 31, max becomes 31. Finally 42 wins — that is the answer.

Live preview

Uses the sample array from example 1: 14, 7, 25, 31, 10, 42.

Expected result: 42.

Live result
Press “Find maximum”.

Algorithm

Goal: return the largest value in a non-empty int[].

Initialize

Set max to arr[0] (after confirming the array is not empty).

Scan

Loop i from 1 to arr.Length - 1.

Update

If arr[i] > max, set max = arr[i].

Return

Return final max.

📜 Pseudocode

Pseudocode
function findMax(arr):
    if arr is empty:
        handle error
    max ← arr[0]
    for i from 1 to arr.length - 1:
        if arr[i] > max:
            max ← arr[i]
    return max
1

Find maximum (reference program)

Classic interview solution: assume a non-empty array, scan once, print the result with string interpolation.

c#
using System;

class Program
{
    static int FindMax(int[] arr)
    {
        int max = arr[0];

        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] > max)
            {
                max = arr[i];
            }
        }

        return max;
    }

    static void Main()
    {
        int[] array = { 14, 7, 25, 31, 10, 42 };

        int maxValue = FindMax(array);

        Console.WriteLine($"Maximum value in the array: {maxValue}");
    }
}

Explanation

FindMax never sorts — it only compares. Starting at index 1 avoids comparing arr[0] with itself. arr.Length is the C# way to get the element count (no separate size variable needed).

2

All elements are negative

Same FindMax logic — the “maximum” is the least negative number. Includes an empty-array guard for safer reuse.

c#
using System;

class Program
{
    static int FindMax(int[] arr)
    {
        if (arr == null || arr.Length == 0)
        {
            throw new ArgumentException("Array must not be null or empty.");
        }

        int max = arr[0];

        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] > max)
            {
                max = arr[i];
            }
        }

        return max;
    }

    static void Main()
    {
        int[] negatives = { -9, -3, -1, -7 };

        Console.WriteLine($"Maximum (least negative): {FindMax(negatives)}");
    }
}

Explanation

-1 is greater than -9, -3, and -7, so it wins. The guard at the top shows good practice when the method might receive bad input.

Beyond the basics

LINQ. For production code on a non-empty array: array.Max() (requires using System.Linq;).

foreach. You can write foreach (int value in arr) instead of indexing — same O(n) scan.

Interview: explain the linear scan first, then mention LINQ only if asked about libraries.

❓ FAQ

It picks an initial candidate from actual data. After one pass, max stores the largest value seen — better than guessing int.MinValue unless you also handle empty arrays.
There is no maximum in an empty array. Check arr.Length (and null) first, then throw, return a nullable, or use a sentinel — but never call arr[0] on an empty array.
Yes. The same comparison logic works, and the largest (least negative) value is returned.
The algorithm returns the maximum value itself; duplicates do not change the answer.
No for this task alone. Sorting is usually O(n log n) — slower than a single O(n) scan when you only need the max.
Yes: array.Max() works for non-empty int arrays. Interviews still expect you to explain the manual loop.
One pass over n elements: O(n) time and O(1) extra space.

🔄 Input / output examples

Input arrayOutput
{ 14, 7, 25, 31, 10, 42 }42
{ -9, -3, -1, -7 }-1
{ 5 } (single element)5
{ 10, 10, 3 } (duplicates)10

Edge cases and pitfalls

Handle special inputs before you read arr[0].

Empty

Length is zero

No first element exists. Throw ArgumentException, return int?, or document that callers must pass a non-empty array.

Single

One element

The loop body never runs; arr[0] is already the answer.

Duplicate

Same max repeated

Result remains the same maximum value — no special case needed.

Sentinel

int.MinValue start

Works only if every value is ≥ int.MinValue. Prefer arr[0] when the array is guaranteed non-empty.

⏱️ Time and space complexity

TaskTimeExtra space
Find maximum in array of size nO(n)O(1)

Every element is compared at most once — optimal for an unsorted array when you only need the max.

Summary

  • Algorithm: track one running maximum while scanning once from left to right.
  • C#: arr.Length, for loop from index 1, FindMax helper method.
  • Complexity: O(n) time, O(1) extra space; works for negatives and duplicates.
Did you know?

Finding the maximum in an unsorted array needs at least n − 1 comparisons in the worst case. A single left-to-right scan achieves 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