Finding Lowest number from a List in C#

Below is a simple program to find the lowest number from a list of numbers –

using System;
using System.Collections.Generic;
 
namespace GroundCSApp
{
    class GetSmallest
    {
        public static void Main(string[] argvs)
        {
            var numlist = new List<int> { 123456 };
 
            var finallst = GetSmallests(numlist, 3);
            foreach (var i in finallst)
            {
                Console.WriteLine(i);
            }
        }
 
        public static List<int> GetSmallests(List<int> lst, int cnt)
        {
            if (cnt > lst.Count)
            {
                throw new ArgumentOutOfRangeException("cnt""Count of numbers is 
greater than the list count");
            }
            var buff = lst;
 
 
            var smalllst = new List<int>();
 
            while (buff.Count > cnt)
            {
                var min = GetiSmallest(buff);
 
                smalllst.Add(min);
                buff.Remove(min);
            }
            return smalllst;
        }
 
        public static int GetiSmallest(List<int> lsti)
        {
            var min = lsti[0];
 
            for (var j = 1; j < lsti.Count; j++)
            {
                if (lsti[j] < min)
                {
                    min = lsti[j];
                }
            }
            return min;
 
        }
 
    }
}

Add a Comment

Your email address will not be published. Required fields are marked *