15,792,397 members
See more:
Hi All,

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

How to get min and max .

Posted
Updated 8-Aug-23 2:33am
v2
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?

## Solution 4

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...

## Solution 1

C#
```var myList = new List<int>();
var min = myList.Min();
var max = myList.Max();```

v2
Mohamed Mitwalli 16-Jul-12 4:50am
5+

## Solution 6

```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();
```

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.

## Solution 7

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.