Find min and max values in a list with index in C#
This post will discuss how to determine the minimum and the maximum value in a list with its index in C#.
1. Using Min() and Max() Method
The standard solution to get the minimum value in a sequence of values is using the Min() method. Similarly to get the maximum value, use the Max() method. The following example demonstrates the usage of the Min() and Max() methods to find the minimum and maximum value in a list of integers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 9, -7, 8 }; Console.WriteLine("Maximum element " + list.Max()); Console.WriteLine("Minimum element " + list.Min()); } } |
Output:
Maximum element 9
Minimum element -7
We can easily extend the above solution to print the indices of the minimum and the maximum values. To get the indices, change the code like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 9, -7, 8 }; int maxValue = list.Max(); int maxIndex = list.IndexOf(maxValue); int minValue = list.Min(); int minIndex = list.IndexOf(minValue); Console.WriteLine("Maximum element {0} present at index {1}", maxValue, maxIndex); Console.WriteLine("Minimum element {0} present at index {1}", minValue, minIndex); } } |
Output:
Maximum element 9 present at index 3
Minimum element -7 present at index 4
2. Using Enumerable.Select Method
The Enumerable.Select method projects each element of a sequence into a new form by incorporating the element’s index. The following example demonstrates the usage of the Select() method to find both value and index to find the minimum and the maximum element in a single line.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System; using System.Linq; using System.Collections.Generic; public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 9, -7, 8 }; var (maxValue, maxIndex) = list.Select((x, i) => (x, i)).Max(); var (minValue, minIndex) = list.Select((x, i) => (x, i)).Min(); Console.WriteLine("Maximum element {0} present at index {1}", maxValue, maxIndex); Console.WriteLine("Minimum element {0} present at index {1}", minValue, minIndex); } } |
Output:
Maximum element 9 present at index 3
Minimum element -7 present at index 4
3. Using Custom Routine
Alternatively, we can write a custom routine to find the minimum and the maximum values in a list. The idea is to iterate through the list and keep track of the minimum and the maximum value so far (and optionally index). Finally, return the minimum and the maximum value. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; public static class Extensions { public static int findMin(this IList<int> items) { int minVal = int.MaxValue; foreach (int i in items) { if (i < minVal) { minVal = i; } } return minVal; } public static int findMax(this IList<int> items) { int maxVal = int.MinValue; foreach (int i in items) { if (i > maxVal) { maxVal = i; } } return maxVal; } } public class Example { public static void Main() { List<int> list = new List<int>() { 5, -1, 4, 9, -7, 8 }; Console.WriteLine("Maximum element " + list.findMax()); Console.WriteLine("Minimum element " + list.findMin()); } } |
Output:
Maximum element 9
Minimum element -7
That’s all about finding the minimum and the maximum values in a list with its index in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)