C# Minimum Value of an Array Program

Beginner
5 min read
Updated: Oct 2026
2 programs
Live preview

What Is the Minimum Value of an Array?

The minimum is the smallest element in the array. For an unsorted array, scan once from left to right and keep a running best.

Remember
arr = [12, 45, 2, 42, 19]
min = 2

Idea:
  min = arr[0]
  for each later element x:
      if x < min → min = x

Works for negatives too:
  [-8, -1, -15] → min = -15
ArrayMinimum
[12, 45, 2, 42, 19]2
[-8, -1, -15]-15
[5]5
[]undefined — handle empty

How to Find the Minimum in C#

Four clear steps — one linear pass is enough (same pattern as maximum, with <).

  1. Reject an empty array (no minimum exists).
  2. Set min = arr[0] as the first candidate.
  3. Loop from index 1 to the end.
  4. Whenever arr[i] < min, update min (optionally track the index too).
MethodIdeaTimeBest for
Linear scanRunning minimum with <O(n)Interviews, unsorted arrays
Track indexSame scan, also store the positionO(n)When you need where the min is

Pseudocode

Pseudocode
function FindMin(arr):
    if length(arr) == 0:
        error "empty array"

    min = arr[0]
    for i from 1 to length(arr) - 1:
        if arr[i] < min:
            min = arr[i]

    return min

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Startmin = arr[0]
Updateif arr[i] < min
EmptyValidate before reading arr[0]
Vs maximumSame loop; flip > to <
AvoidSorting just to get the min
Classic check[12, 45, 2, 42, 19] → 2

Live Preview

Pick a sample array to see the running minimum after each step.

Tap a chip to run the linear scan trace.

Result
arr = [12, 45, 2, 42, 19]
start min = 12
45 ≥ 12
2  < 12 → min = 2
42 ≥ 2
19 ≥ 2
Minimum = 2

Worked Walkthrough — [12, 45, 2, 42, 19]

Start at the first element and update whenever a smaller value appears.

Calculation
min = 12

i=1 → 45 ≥ 12 → no change
i=2 → 2  < 12 → min = 2
i=3 → 42 ≥ 2  → no change
i=4 → 19 ≥ 2  → no change

Minimum = 2

C# Programs

Start with a simple running minimum. Then track the index when you also need the position.

Example 1 — Linear Scan

Keep a running minimum and update it whenever a smaller element appears.

C#
using System;

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

        int min = arr[0];
        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] < min)
            {
                min = arr[i];
            }
        }
        return min;
    }

    static void Main()
    {
        int[] numbers = { 12, 45, 2, 42, 19 };
        Console.WriteLine("Minimum = " + FindMin(numbers));
    }
}

How It Works

  1. Reject null or empty arrays before reading arr[0].
  2. Seed min with the first element.
  3. Loop from index 1 through the rest of the array.
  4. Update when arr[i] < min.
  5. Return the final running minimum after one pass.

Example 2 — Track Index

Same scan, but also remember the first index where the minimum appears.

C#
using System;

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

        int minIndex = 0;
        for (int i = 1; i < arr.Length; i++)
        {
            if (arr[i] < arr[minIndex])
            {
                minIndex = i;
            }
        }
        return minIndex;
    }

    static void Main()
    {
        int[] numbers = { 12, 45, 2, 42, 19 };
        int index = FindMinIndex(numbers);
        Console.WriteLine("Min = " + numbers[index] + " at index " + index);
    }
}

How It Works

  1. Start with minIndex = 0 (first element is the candidate).
  2. Compare each later element to arr[minIndex].
  3. When a smaller value appears, store that new index.
  4. Ties keep the earlier index because the test uses strict <.
  5. Read the value with numbers[index] after the scan.

Edge Cases & Pitfalls

Check these before calling the solution done.

Empty

No minimum

Validate length before using arr[0].

Seed with 0

Wrong for positives

Seeding min = 0 fails when every element is positive.

Duplicates

Same answer

Multiple copies of the min still return that value.

Sorting

Overkill

One scan is enough; sorting is usually slower for this goal.

Time and Space Complexity

ProgramTimeExtra space
Linear scanO(n)O(1)
Track indexO(n)O(1)

You need about n − 1 comparisons in the worst case. One pass is optimal for an unsorted array.

Key Takeaways

  • Pattern: seed with arr[0], then scan with <.
  • Empty: validate before reading the first element.
  • Index: same loop can track where the min sits.
  • Test: [12, 45, 2, 42, 19] → 2.

One line: one pass, keep the smaller value — that is the minimum.

Frequently Asked Questions

The first element is a valid candidate. Then every later element is compared against that running minimum.
An empty array has no minimum. Validate Length > 0 and throw or return early.
Yes. The minimum is still the smallest value (for example 2 in [12, 45, 2, 19]).
No. Sorting is usually slower than one linear scan when you only need the minimum.
One pass over n elements: O(n) time and O(1) extra space.

Next: Multiplication Table

Print a multiplication table for a given number.

Multiplication table tutorial →

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.

10 people found this page helpful