Find Minimum 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 minimum while you walk the array once.
  • How to write FindMin with foreach or a for loop.
  • 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 foreach or for (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.

Start min = arr[0]
Update if value < min
vs max flip > to <

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.

Expected result: 2.

Live result
Press “Find minimum”.

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

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
1

Find minimum (reference program)

Uses foreach to walk every element — a readable C# style that matches the classic reference solution.

c#
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.

2

All elements are negative

Same logic with a for loop and an empty-array guard — the minimum is the most negative value.

c#
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

It means the smallest value among all elements in the array.
It gives a valid initial candidate from the data itself — better than guessing int.MaxValue unless you also handle empty arrays.
There is no minimum in an empty array. Check arr.Length (and null) first before reading arr[0].
Yes. The smallest (most negative) value is still found correctly.
The algorithm returns the minimum value itself; duplicates do not change the answer.
Yes. foreach (int element in arr) with element < min works. A for loop from index 1 avoids comparing arr[0] with itself.
One pass over n elements: O(n) time and O(1) extra space.

🔄 Input / output examples

Input arrayOutput
{ 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].

Empty

Length is zero

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

Single

One element

The loop may not run; arr[0] is already the answer.

Duplicate

Same min repeated

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

Sentinel

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

TaskTimeExtra space
Find minimum 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 min.

Summary

  • Algorithm: track one running minimum while scanning once from left to right.
  • C#: foreach or for, compare with <, FindMin helper.
  • Complexity: O(n) time, O(1) extra space; mirror of the maximum scan.
Did you know?

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.

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