Related Articles

# Dart – Finding Minimum and Maximum Value in a List

• Last Updated : 20 Jul, 2020

In Dart we can find the minimum and maximum valued element present in the given list in seven ways:

1. Using for loop to find the largest and smallest element.
2. Using sort function to find the largest and smallest element.
3. Using forEach loop to find the largest and smallest element.
4. Using only reduce method in dart to find the largest and smallest element.
5. Using reduce method with dart:math library.
6. Using fold method with dart to find the largest and smallest element.
7. Using fold method with dart:math library.

### Using for loop

It is the most basic way to find the largest and smallest element present in the list by simply going through all the elements comparing them and giving the answer.

Example:

## Dart

 `// Main function``void` `main() {``   ` `  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Declaring and assigning the``  ``// largestGeekValue and smallestGeekValue``  ``var largestGeekValue = geekList;``  ``var smallestGeekValue = geekList;`` ` `  ``for` `(var i = 0; i < geekList.length; i++) {``     ` `    ``// Checking for largest value in the list``    ``if` `(geekList[i] > largestGeekValue) {``      ``largestGeekValue = geekList[i];``    ``}``     ` `    ``// Checking for smallest value in the list``    ``if` `(geekList[i] < smallestGeekValue) {``      ``smallestGeekValue = geekList[i];``    ``}``  ``}`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \$smallestGeekValue"``);``  ``print(``"Largest value in the list : \$largestGeekValue"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121
```

### Using sort function

Dart also provides the user to sort the list in the ascending order i.e. first one is the smallest and last one is largest.

Example:

## Dart

 `// Main function``void` `main() {``  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Sorting the list``  ``geekList.sort();`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \${geekList.first}"``);``  ``print(``"Largest value in the list : \${geekList.last}"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121
```

## Using forEach loop

Unlike for loop, one can also use forEach loop to get the elements of the list and then check for the conditions in the elements of the list.

Example:

## Dart

 `// Main function``void` `main() {``   ` `  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Declaring and assigning the``  ``// largestGeekValue and smallestGeekValue``  ``var largestGeekValue = geekList;``  ``var smallestGeekValue = geekList;``   ` `  ``// Using forEach loop to find``  ``// the largest and smallest``  ``// numbers in the list``  ``geekList.forEach((gfg) => {``        ``if` `(gfg > largestGeekValue) {largestGeekValue = gfg},``        ``if` `(gfg < smallestGeekValue) {smallestGeekValue = gfg},``      ``});`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \$smallestGeekValue"``);``  ``print(``"Largest value in the list : \$largestGeekValue"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121```

## Using reduce method

One can use Dart reduce function to reduce the list to a particular value and save it.

Example:

## Dart

 `// Main function``void` `main() {``   ` `  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Declaring and assigning the``  ``// largestGeekValue and smallestGeekValue``  ``// Finding the smallest and largest``  ``// value in the list``  ``var smallestGeekValue = geekList.reduce(``  ``(current, next) => current < next ? current : next); ``  ``var largestGeekValue = geekList.reduce(``  ``(current, next) => current > next ? current : next);`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \$smallestGeekValue"``);``  ``print(``"Largest value in the list : \$largestGeekValue"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121
```

## Using reduce method with dart:math library

The above code can be minimized by importing the math libraries.

Example:

## Dart

 `import ``"dart:math"``;`` ` `// Main function``void` `main() {``  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Declaring and assigning the``  ``// largestGeekValue and smallestGeekValue``  ``// Finding the smallest and largest value in the list``  ``var smallestGeekValue = geekList.reduce(min); ``  ``var largestGeekValue = geekList.reduce(max);`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \$smallestGeekValue"``);``  ``print(``"Largest value in the list : \$largestGeekValue"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121
```

## Using fold method

Apart from reducing dart also has fold method which is quite similar to reduce apart from the fact that it starts with an initial value and then changes it during the process.

Example:

## Dart

 `// Main function``void` `main() {``   ` `  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Declaring and assigning the``  ``// largestGeekValue and smallestGeekValue``  ``// Finding the smallest and``  ``// largest value in the list``  ``var smallestGeekValue = geekList.fold(geekList,``  ``(previous, current) => previous < current ? previous : current);``  ``var largestGeekValue = geekList.fold(geekList,``  ``(previous, current) => previous > current ? previous : current);`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \$smallestGeekValue"``);``  ``print(``"Largest value in the list : \$largestGeekValue"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121
```

## Using fold method with dart:math library

The above code can be minimized importing the math libraries.

Example:

## Dart

 `import ``"dart:math"``;`` ` `// Main function``void` `main() {``  ``// Creating a geek list``  ``var geekList = [121, 12, 33, 14, 3];``   ` `  ``// Declaring and assigning``  ``// the largestGeekValue and smallestGeekValue``  ``// Finding the smallest and``  ``// largest value in the list``  ``var smallestGeekValue = geekList.fold(geekList,min);``  ``var largestGeekValue = geekList.fold(geekList,max);`` ` `  ``// Printing the values``  ``print(``"Smallest value in the list : \$smallestGeekValue"``);``  ``print(``"Largest value in the list : \$largestGeekValue"``);``}`

Output:

```Smallest value in the list : 3
Largest value in the list : 121
```

My Personal Notes arrow_drop_up