0
0
CsharpProgramBeginner · 2 min read

C# Program to Find Largest Number in Array

To find the largest number in an array in C#, use a loop to compare each element and keep track of the maximum value, like int max = arr[0]; foreach (int num in arr) { if (num > max) max = num; }.
📋

Examples

Input[3, 5, 1, 9, 2]
Output9
Input[10, 10, 10]
Output10
Input[-5, -1, -10]
Output-1
🧠

How to Think About It

To find the largest number, start by assuming the first number is the largest. Then check each number one by one. If you find a number bigger than your current largest, update it. At the end, the largest number you have is the biggest in the array.
📐

Algorithm

1
Start with the first element as the largest number.
2
Go through each number in the array.
3
If the current number is bigger than the largest, update the largest.
4
After checking all numbers, return the largest number.
💻

Code

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

Dry Run

Let's trace the array [3, 5, 1, 9, 2] through the code to find the largest number.

1

Initialize max

max = 3 (first element)

2

Compare with 5

5 > 3, so max = 5

3

Compare with 1

1 > 5? No, max stays 5

4

Compare with 9

9 > 5, so max = 9

5

Compare with 2

2 > 9? No, max stays 9

6

Result

Largest number is 9

Current Numbermax Value
33
55
15
99
29
💡

Why This Works

Step 1: Start with first element

We assume the first number is the largest to have a starting point for comparison.

Step 2: Compare each element

We check each number using if (num > max) to find if it is bigger than the current largest.

Step 3: Update largest value

When a bigger number is found, we update max to that number to keep track of the largest.

Step 4: Final largest number

After checking all numbers, max holds the largest number in the array.

🔄

Alternative Approaches

Using LINQ Max() method
csharp
using System;
using System.Linq;
class Program {
    static void Main() {
        int[] arr = {3, 5, 1, 9, 2};
        int max = arr.Max();
        Console.WriteLine(max);
    }
}
This is the shortest and most readable way but requires using LINQ and may be less clear for beginners.
Using for loop with index
csharp
using System;
class Program {
    static void Main() {
        int[] arr = {3, 5, 1, 9, 2};
        int max = arr[0];
        for (int i = 1; i < arr.Length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        Console.WriteLine(max);
    }
}
This uses a classic for loop instead of foreach, useful if you need the index for other reasons.

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

Time Complexity

The program checks each element once, so the time grows linearly with the 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?

Using a simple loop or LINQ's Max() both run in O(n) time, but LINQ is more concise while loops give more control.

ApproachTimeSpaceBest For
Foreach loopO(n)O(1)Clear logic and beginner-friendly
For loop with indexO(n)O(1)When index is needed for extra logic
LINQ Max()O(n)O(1)Concise code and readability
💡
Always initialize the largest value with the first element of the array to avoid errors.
⚠️
A common mistake is starting the largest value at zero, which fails if all numbers are negative.