VOOZH about

URL: https://www.geeksforgeeks.org/dart/dart-finding-minimum-and-maximum-value-in-a-list/

⇱ Dart - Finding Minimum and Maximum Value in a List - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Dart - Finding Minimum and Maximum Value in a List

Last Updated : 7 Apr, 2025

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

  • Use the for loop to find the largest and smallest elements.
  • Use the sort function to find the largest and smallest elements.
  • Using forEach loop to find the largest and smallest element.
  • Using only the reduce method in dart to find the largest and smallest element.
  • Using reduce method with dart:math library.
  • Using the fold method with dart to find the largest and smallest element.
  • Using the fold method with dart:math library.

1. Using the 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:

Output:

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


2. Using the 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:

Output:

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


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

Output:

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


4. Using the reduce() method

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

Example:

Output:

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


5. Using the reduce() method with dart:math library

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

Example:

Output:

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


6. Using the 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:

Output:

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


7. Using the fold() method with dart:math library

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

Example:

Output:

Smallest value in the list : 3
Largest value in the list : 121
Comment
Article Tags:
Article Tags:

Explore