0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Smallest Element in Array

To find the smallest element in an array in C#, use a loop to compare elements and keep track of the smallest value, like int min = arr[0]; foreach (int num in arr) { if (num < min) min = num; }.
📋

Examples

Input[5, 3, 8, 1, 4]
Output1
Input[10, 20, 30, 40]
Output10
Input[-2, -5, -1, -9]
Output-9
🧠

How to Think About It

To find the smallest number in an array, start by assuming the first number is the smallest. Then, look at each number one by one. If you find a number smaller than your current smallest, remember it. After checking all numbers, the one you remembered is the smallest.
📐

Algorithm

1
Start with the first element as the smallest value.
2
Look at each element in the array one by one.
3
If an element is smaller than the current smallest, update the smallest value.
4
After checking all elements, return the smallest value.
💻

Code

csharp
using System;
class Program {
    static void Main() {
        int[] arr = {5, 3, 8, 1, 4};
        int min = arr[0];
        foreach (int num in arr) {
            if (num < min) {
                min = num;
            }
        }
        Console.WriteLine(min);
    }
}
Output
1
🔍

Dry Run

Let's trace the array [5, 3, 8, 1, 4] through the code to find the smallest number.

1

Initialize min

min = 5 (first element)

2

Compare with 3

3 < 5, so min = 3

3

Compare with 8

8 < 3? No, min stays 3

4

Compare with 1

1 < 3, so min = 1

5

Compare with 4

4 < 1? No, min stays 1

6

Result

Smallest number is 1

Current NumberCurrent Min
55
33
83
11
41
💡

Why This Works

Step 1: Start with first element

We assume the first element is the smallest to have a starting point for comparison.

Step 2: Compare each element

We check each number using if (num < min) to find if there's a smaller one.

Step 3: Update smallest value

When a smaller number is found, we update min to that number.

Step 4: Return smallest

After checking all elements, the min holds the smallest number in the array.

🔄

Alternative Approaches

Using Array.Sort()
csharp
using System;
class Program {
    static void Main() {
        int[] arr = {5, 3, 8, 1, 4};
        Array.Sort(arr);
        Console.WriteLine(arr[0]);
    }
}
This sorts the array first, then picks the first element as smallest; simpler but modifies the array.
Using LINQ Min() method
csharp
using System;
using System.Linq;
class Program {
    static void Main() {
        int[] arr = {5, 3, 8, 1, 4};
        int min = arr.Min();
        Console.WriteLine(min);
    }
}
This uses built-in LINQ method for simplicity and readability; requires System.Linq namespace.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks each element once, so time grows linearly with array size, making it O(n).

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The loop method and LINQ Min() both run in O(n), but sorting is slower at O(n log n) and changes the array.

ApproachTimeSpaceBest For
Loop with comparisonO(n)O(1)Fastest, no extra memory, original array unchanged
Array.Sort()O(n log n)O(1)Simple but slower and modifies array
LINQ Min()O(n)O(1)Clean code, requires LINQ, original array unchanged
💡
Always initialize the smallest value with the first element of the array before comparing.
⚠️
A common mistake is starting with zero or another number instead of the first array element, which can give wrong results.