Question #3: Find a Nth highest or lowest number in a given series of numbers.
Solution 1:
Use
Min() and Max() Extension Methods of Enumerable Class that resides under the namespace
System.Linq
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<int> lstInts = new List<int>() { 1, 206, 34, -4, 590, 61, 78, 8, 9, 180,237 };
int minNum = lstInts.Min(); //lowest number
int maxNum = lstInts.Max(); //highest number
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);
Console.ReadKey(true);
}
}
}
Result
Min = -4 , Max = 590
OR Since you asked for numbers which can be +ve,-ve, fractional (not considering Complex numbers here), you can modify the above program as under
List<double> lstNumbers= new List<double>() { 1, 2.06, 34, -4, 5.90, 61.9, 7.8, 8, 9, 18.0,23.7 };
double minNum = lstNumbers.Min(); //lowest number
double maxNum = lstNumbers.Max(); //highest number
Console.WriteLine("Min = {0} , Max = {1}", minNum, maxNum);
Hope this helps
--
Thanks & Regards,
RNA Team
Shreedar, if this helps please login to Mark As Answer. | Alert Moderator