Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum integer value find in list<int>

Tags:

c#

I have a List<int> with several elements. I know I can get all the values if I iterate through it with foreach, but I just want the maximum int value in the list.

var l = new List<int>() { 1, 3, 2 }; 
like image 896
kumarsram Avatar asked Mar 14 '11 05:03

kumarsram


People also ask

How do you find the maximum value of a list?

Method: Use the max() and def functions to find the largest element in a given list. The max() function prints the largest element in the list.

How do you find maximum integers?

To find the max value for the unsigned integer data type, we take 2 to the power of 16 and substract by 1, which would is 65,535 . We get the number 16 from taking the number of bytes that assigned to the unsigned short int data type (2) and multiple it by the number of bits assigned to each byte (8) and get 16.

How do you find the largest value in a list Python?

In Python, there is a built-in function max() you can use to find the largest number in a list. To use it, call the max() on a list of numbers. It then returns the greatest number in that list.


2 Answers

Assuming .NET Framework 3.5 or greater:

var l = new List<int>() { 1, 3, 2 }; var max = l.Max(); Console.WriteLine(max); // prints 3 

Lots and lots of cool time-savers like these in the Enumerable class.

like image 168
Michael Petrotta Avatar answered Sep 22 '22 16:09

Michael Petrotta


Use Enumerable.Max

int max = l.Max(); 
like image 44
Yuriy Faktorovich Avatar answered Sep 21 '22 16:09

Yuriy Faktorovich