Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. This article also participated in the “Digitalstar Project” to win a creative gift package and creative incentive money
In Dart, there are seven ways to find the minimum and maximum elements present in a given list:
- Use the for loop to find the maximum and minimum elements.
- Use the sort function to find the largest and smallest elements.
- Use forEach loops to find maximum and minimum elements.
- Only the reduce method is used in DART to find the largest and smallest elements.
- Use the Reduce method in the DART: Math library.
- Use the FOLD method with DART to find the maximum and minimum elements.
- Use the fold method in the DART: Math library.
useThe for loop
This is the most basic way to find the largest and smallest elements in a list, just by going through all the elements and comparing them and giving an answer.
Example:
// 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[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");
}
Copy the code
usesorting
Dart also allows users to sort lists in ascending order, with the first smallest and the last largest.
Example: *
// 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}");
}
Copy the code
useThe forEach loop
Unlike the for loop, you can also use the forEach loop to get the list elements and then check the conditions in the list elements.
// 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[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");
}
Copy the code
useThe reduce method
You can use the Dart Reduce function to reduce the list to a specific value and save it.
// 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"); }Copy the code
indart:mathUsed in the libraryThe reduce method
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");
}
Copy the code
useA fold method
In addition to reducing darts there is folding, which is very similar to reducing, except that you start with an initial value and then change it along the way.
Example:
// 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[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"); }Copy the code
indart:mathUsed in the libraryA fold method
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[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"); }Copy the code
1
\