Find Maximum Value of an Array in C#
What you’ll learn
- How a linear scan keeps a running maximum while you walk the array once.
- How to write a reusable
FindMaxmethod witharr.Lengthand aforloop. - 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 readarr[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.
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.
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
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 Find maximum (reference program)
Classic interview solution: assume a non-empty array, scan once, print the result with string interpolation.
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).
All elements are negative
Same FindMax logic — the “maximum” is the least negative number. Includes an empty-array guard for safer reuse.
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
🔄 Input / output examples
| Input array | Output |
|---|---|
{ 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].
Length is zero
No first element exists. Throw ArgumentException, return int?, or document that callers must pass a non-empty array.
One element
The loop body never runs; arr[0] is already the answer.
Same max repeated
Result remains the same maximum value — no special case needed.
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
| Task | Time | Extra space |
|---|---|---|
Find maximum in array of size n | O(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,forloop from index1,FindMaxhelper method. - Complexity:
O(n)time,O(1)extra space; works for negatives and duplicates.
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.
8 people found this page helpful
