Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find highest integer in a Generic List using C#?

Tags:

c#

.net-3.5

I have the following List<int> collection and I need to find the highest integer in the collection. It could have an arbitrary number of integers and I can have same integer value for multiple times.

List<int> MyList = new List<int> { 3, 4, 6, 7, 9, 3, 4, 5, 5 };

What is the simplest algorithm to use for finding the highest integer? I am using C# and the .NET 3.5 framework.

like image 429
Michael Kniskern Avatar asked Jun 23 '09 18:06

Michael Kniskern


People also ask

How to get largest number in list c#?

You can just do: int max = MyList. Max();

How to get minimum value in list c#?

To get the smallest element, use the Min() method. list. AsQueryable(). Min();


1 Answers

You can just do:

int max = MyList.Max();

See Enumerable.Max for details.

like image 163
Reed Copsey Avatar answered Sep 29 '22 11:09

Reed Copsey