Dart – Finding Minimum and Maximum Value in a List

20 Jul, 2020
In Dart we can find the minimum and maximum valued element present in the given list in seven ways:
Using for loop to find the largest and smallest element.Using sort function to find the largest and smallest element.Using forEach loop to find the largest and smallest element.Using only reduce method in dart to find the largest and smallest element.Using reduce method with dart:math library.Using fold method with dart to find the largest and smallest element.Using fold method with dart:math library.
Using for loop to find the largest and smallest element.
Using sort function to find the largest and smallest element.
Using forEach loop to find the largest and smallest element.
Using only reduce method in dart to find the largest and smallest element.
Using reduce method with dart:math library.
Using fold method with dart to find the largest and smallest element.
Using fold method with dart:math library.
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 functionvoid main() {      // Creating a geek list  var geekList = [121, 12, 33, 14, 3];      // Declaring and assigning the  // largestGeekValue and smallestGeekValue  var largestGeekValue = geekList[0];  var smallestGeekValue = geekList[0];    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

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

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 functionvoid main() {      // Creating a geek list  var geekList = [121, 12, 33, 14, 3];      // Declaring and assigning the  // largestGeekValue and smallestGeekValue  var largestGeekValue = geekList[0];  var smallestGeekValue = geekList[0];      // 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
One can use Dart reduce function to reduce the list to a particular value and save it.
Example:
Dart
// Main functionvoid 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

The above code can be minimized by importing the math libraries.
Example:
Dart
import "dart:math";  // Main functionvoid 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

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 functionvoid 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[0],  (previous, current) => previous < current ? previous : current);  var largestGeekValue = geekList.fold(geekList[0],  (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

The above code can be minimized importing the math libraries.
Example:
Dart
import "dart:math";  // Main functionvoid 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[0],min);  var largestGeekValue = geekList.fold(geekList[0],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

Dart-List
Dart
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Flutter - DropDownButton Widget
Flutter - Custom Bottom Navigation Bar
Flutter - Checkbox Widget
ListView Class in Flutter
Flutter - Row and Column Widgets
Dart Tutorial
Flutter - Stack Widget
Flutter - Search Bar
Container class in Flutter
Operators in Dart