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 };
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.
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.
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.
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.
Use Enumerable.Max
int max = l.Max();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With