Click here to Skip to main content
15,792,397 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi All,

I have list having numbers .123,456,487,984..... and so on.

How to get min and max .

Please help.
Updated 8-Aug-23 2:33am
bbirajdar 16-Jul-12 4:18am    
What have you tried?
Sandeep Mewara 16-Jul-12 4:45am    
what have you tried so far? where are you stuck?

Since this is your homework, you will get no code!
But it is a really, really, easy task. There are two basic approaches you can take:
1) Sort the list of numbers. The min in the first in the list, the max is the last.
2) Set up two variables called max and min. Set max to the smallest (or most negative) value it can hold, set min to teh largest value. The loop through each value in your list and compare it with these values. If it is larger than max, set max to it. if it is smaller than min, set min to it. After the look, you have the max and min values.

Neither of these approaches should take you more than a dozen lines of code. In fact, if you are clever, the first approach can be just three lines of code...
Share this answer
var myList = new List<int>();
var min = myList.Min();
var max = myList.Max();
Share this answer
Mohamed Mitwalli 16-Jul-12 4:50am    
List<int> Numbers =new List<int>();
//represents minimum value in the list
int MinValue = Numbers.OrderBy(p => p).FirstOrDefault();
//represents maximum value in the list
int MaxValue = Numbers.OrderByDesending(p => p).FirstOrDefault();
Share this answer
Patrice T 2-Apr-20 16:43pm    
Not only you are 8 years too late, but your solution is highly inefficient.
Sorting a list just to get Max value and sorting again for Min is overkill.
S1 provided 8 years ago is much more efficient.
Member 11898005 2-Apr-20 17:44pm    
so what would be the best solution
Patrice T 2-Apr-20 20:21pm    
see solution 1.
The anwer depends on usage of the list. If the list is given The Max and Min method are good.
If frequently You add new values to the list just collect the min and max in variables.
If the same values are added other are deleted the fastest be additional heap structer one for min, second for max.
Share this answer
Dave Kreskowiak 8-Aug-23 9:37am    
Look at the date on the question. You're 11 years too late and the OP posted a homework assignment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900