Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
Finding the largest, smallest, second largest, and second smallest elements in a list is a common programming task in C#. Using LINQ methods like Max(), Min(), OrderBy(), and Skip() provides an elegant solution for these operations.
Syntax
To find the largest element −
list.Max()
To find the smallest element −
list.Min()
To find the second largest element −
list.OrderByDescending(x => x).Skip(1).First()
To find the second smallest element −
list.OrderBy(x => x).Skip(1).First()
Using LINQ Methods
The most straightforward approach uses LINQ extension methods to find all four values −
using System;
using System.Linq;
public class Program {
public static void Main() {
var val = new int[] {
99,
35,
26,
87
};
var maxNum = val.Max();
Console.WriteLine("Maximum Number: " + maxNum);
var minNum = val.Min();
Console.WriteLine("Minimum Number: " + minNum);
var secondMax = val.OrderByDescending(z => z).Skip(1).First();
Console.WriteLine("Second Largest Number: " + secondMax);
var secondMin = val.OrderBy(z => z).Skip(1).First();
Console.WriteLine("Second Smallest Number: " + secondMin);
}
}
The output of the above code is −
Maximum Number: 99 Minimum Number: 26 Second Largest Number: 87 Second Smallest Number: 35
Using Distinct Values
When the list contains duplicate values, you may want to find distinct second largest and smallest values −
using System;
using System.Linq;
public class Program {
public static void Main() {
var numbers = new int[] { 99, 87, 99, 35, 26, 87, 35 };
var distinctNumbers = numbers.Distinct().ToList();
Console.WriteLine("Original: [" + string.Join(", ", numbers) + "]");
Console.WriteLine("Distinct: [" + string.Join(", ", distinctNumbers.OrderByDescending(x => x)) + "]");
var maxNum = distinctNumbers.Max();
var minNum = distinctNumbers.Min();
var secondMax = distinctNumbers.OrderByDescending(x => x).Skip(1).First();
var secondMin = distinctNumbers.OrderBy(x => x).Skip(1).First();
Console.WriteLine("Maximum: " + maxNum);
Console.WriteLine("Minimum: " + minNum);
Console.WriteLine("Second Largest: " + secondMax);
Console.WriteLine("Second Smallest: " + secondMin);
}
}
The output of the above code is −
Original: [99, 87, 99, 35, 26, 87, 35] Distinct: [99, 87, 35, 26] Maximum: 99 Minimum: 26 Second Largest: 87 Second Smallest: 35
Using Manual Iteration
For better performance with large datasets, you can use a single-pass algorithm −
using System;
public class Program {
public static void Main() {
int[] numbers = { 99, 35, 26, 87, 45, 12, 78 };
int max = int.MinValue, secondMax = int.MinValue;
int min = int.MaxValue, secondMin = int.MaxValue;
foreach (int num in numbers) {
if (num > max) {
secondMax = max;
max = num;
} else if (num > secondMax && num != max) {
secondMax = num;
}
if (num
The output of the above code is −
Array: [99, 35, 26, 87, 45, 12, 78]
Largest: 99
Smallest: 12
Second Largest: 87
Second Smallest: 26
Comparison of Methods
| Method | Time Complexity | Advantages | Disadvantages |
|---|---|---|---|
| LINQ Methods | O(n log n) | Clean, readable code | Multiple passes through data |
| LINQ with Distinct | O(n log n) | Handles duplicates correctly | Extra memory for distinct values |
| Manual Iteration | O(n) | Single pass, best performance | More complex logic |
Conclusion
C# provides multiple approaches to find largest, smallest, second largest, and second smallest values in a list. LINQ methods offer clean and readable solutions, while manual iteration provides better performance for large datasets. Choose the approach that best fits your requirements for code readability and performance.
