C# Program to Find Second Largest Number in Array
int largest = int.MinValue, secondLargest = int.MinValue; foreach (int num in arr) { if (num > largest) { secondLargest = largest; largest = num; } else if (num > secondLargest && num != largest) { secondLargest = num; } }.Examples
How to Think About It
Algorithm
Code
using System; class Program { static void Main() { int[] arr = {3, 5, 1, 2, 4}; int largest = int.MinValue, secondLargest = int.MinValue; foreach (int num in arr) { if (num > largest) { secondLargest = largest; largest = num; } else if (num > secondLargest && num != largest) { secondLargest = num; } } if (secondLargest == int.MinValue) { Console.WriteLine("No second largest element"); } else { Console.WriteLine(secondLargest); } } }
Dry Run
Let's trace the array [3, 5, 1, 2, 4] through the code to find the second largest number.
Initialize variables
largest = -2147483648, secondLargest = -2147483648
Check first element 3
3 > largest (-2147483648), so largest = 3, secondLargest = -2147483648
Check second element 5
5 > largest (3), so secondLargest = 3, largest = 5
Check third element 1
1 > secondLargest (3)? No, no change
Check fourth element 2
2 > secondLargest (3)? No, no change
Check fifth element 4
4 > secondLargest (3) and 4 != largest (5), so secondLargest = 4
End of loop
largest = 5, secondLargest = 4
| Iteration | Current Number | Largest | Second Largest |
|---|---|---|---|
| 1 | 3 | 3 | -2147483648 |
| 2 | 5 | 5 | 3 |
| 3 | 1 | 5 | 3 |
| 4 | 2 | 5 | 3 |
| 5 | 4 | 5 | 4 |
Why This Works
Step 1: Track largest and second largest
We keep two variables to remember the biggest and second biggest numbers found so far as we look through the array.
Step 2: Update when bigger number found
If a number is bigger than the current largest, we move the largest to second largest and update largest to this new number.
Step 3: Update second largest carefully
If a number is not bigger than largest but bigger than second largest and not equal to largest, we update second largest to this number.
Alternative Approaches
using System; using System.Linq; class Program { static void Main() { int[] arr = {3, 5, 1, 2, 4}; var distinctSorted = arr.Distinct().OrderByDescending(x => x).ToArray(); if (distinctSorted.Length < 2) { Console.WriteLine("No second largest element"); } else { Console.WriteLine(distinctSorted[1]); } } }
using System; using System.Linq; class Program { static void Main() { int[] arr = {3, 5, 1, 2, 4}; int max = arr.Max(); var filtered = arr.Where(x => x != max); if (!filtered.Any()) { Console.WriteLine("No second largest element"); } else { Console.WriteLine(filtered.Max()); } } }
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through the array once, so the time grows linearly with the number of elements, making it O(n).
Space Complexity
Only a few variables are used to track values, so the extra space is constant, O(1).
Which Approach is Fastest?
The single pass method is fastest because it only loops once. Sorting or multiple LINQ calls take more time.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Single pass tracking | O(n) | O(1) | Large arrays, best performance |
| Sort and pick second last | O(n log n) | O(n) | Small arrays or when sorting needed anyway |
| LINQ Max and filter | O(n) | O(n) | Readable code, small to medium arrays |