![]() |
VOOZH | about |
In Dart, we can find the minimum and maximum valued element present in the given list in seven ways:
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
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
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
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
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
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
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