Find Minimum Value of an Array in C#
What you’ll learn
- How a linear scan keeps a running minimum while you walk the array once.
- How to write
FindMinwithforeachor aforloop. - Why the same logic works for negatives and duplicates, and how to guard against an empty array.
Overview
Finding the minimum mirrors finding the maximum — only the comparison flips from > to <. One pass, no sorting, constant extra space.
One pass
O(n) scan — same pattern as max.
Live preview
Sample array — result 2.
Pair with max
Run two scans or one loop tracking both min and max.
Prerequisites
int[] arrays, loops, comparisons, and Console.WriteLine. The maximum-of-array tutorial uses the same scan pattern.
- You can iterate with
foreachorfor (int i = 0; i < arr.Length; i++). - You understand that minimum means the smallest numeric value in the list.
The idea
Keep one current winner (min). Scan left to right and replace it whenever you see a smaller value.
One pass is enough. After visiting every element, min holds the answer.
Walk-through
For {12, 5, 7, 3, 2, 8, 10}: start with min = 12. After seeing 5, then 3, min drops to 3. Finally 2 wins — that is the answer.
Live preview
Uses the sample array from example 1: 12, 5, 7, 3, 2, 8, 10.
Algorithm
Goal: return the smallest value in a non-empty int[].
Initialize
Set min to arr[0] (after confirming the array is not empty).
Scan
Visit every remaining element with foreach or a for loop from index 1.
Update
If the current value is smaller, set min to that value.
Return
Return final min.
📜 Pseudocode
function findMin(arr):
if arr is empty:
handle error
min ← arr[0]
for i from 1 to arr.length - 1:
if arr[i] < min:
min ← arr[i]
return min Find minimum (reference program)
Uses foreach to walk every element — a readable C# style that matches the classic reference solution.
using System;
class Program
{
static int FindMin(int[] arr)
{
int minValue = arr[0];
foreach (int element in arr)
{
if (element < minValue)
{
minValue = element;
}
}
return minValue;
}
static void Main()
{
int[] array = { 12, 5, 7, 3, 2, 8, 10 };
int minValue = FindMin(array);
Console.WriteLine($"The minimum value in the array is: {minValue}");
}
} Explanation
foreach compares every element (including the first) with the running minimum. Starting at arr[0] is still correct — the first comparison is a no-op when values match. A for loop from index 1 skips that redundant check.
All elements are negative
Same logic with a for loop and an empty-array guard — the minimum is the most negative value.
using System;
class Program
{
static int FindMin(int[] arr)
{
if (arr == null || arr.Length == 0)
{
throw new ArgumentException("Array must not be null or empty.");
}
int minValue = arr[0];
for (int i = 1; i < arr.Length; i++)
{
if (arr[i] < minValue)
{
minValue = arr[i];
}
}
return minValue;
}
static void Main()
{
int[] negatives = { -9, -3, -1, -7 };
Console.WriteLine($"Minimum (most negative): {FindMin(negatives)}");
}
} Explanation
-9 is less than -3, -1, and -7, so it wins. The guard prevents IndexOutOfRangeException when arr[0] would not exist.
Beyond the basics
LINQ. For production code on a non-empty array: array.Min() (requires using System.Linq;).
Min and max together. One loop can track both min and max if you need both endpoints.
Interview: write the linear scan first, then mention LINQ only if asked.
❓ FAQ
🔄 Input / output examples
| Input array | Output |
|---|---|
{ 12, 5, 7, 3, 2, 8, 10 } | 2 |
{ -9, -3, -1, -7 } | -9 |
{ 5 } (single element) | 5 |
{ 3, 3, 1 } (duplicates) | 1 |
Edge cases and pitfalls
Handle special inputs before you read arr[0].
Length is zero
No minimum exists. Throw ArgumentException, return int?, or document that callers must pass a non-empty array.
One element
The loop may not run; arr[0] is already the answer.
Same min repeated
Result remains the same minimum value — no special case needed.
int.MaxValue start
Works only if every value is ≤ int.MaxValue. Prefer arr[0] when the array is guaranteed non-empty.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
Find minimum 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 min.
Summary
- Algorithm: track one running minimum while scanning once from left to right.
- C#:
foreachorfor, compare with<,FindMinhelper. - Complexity:
O(n)time,O(1)extra space; mirror of the maximum scan.
Finding the minimum 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
