Suppose we have a list of integers
final numbers = [4, 2, 8, 3, 1, 5, 7, 9];
We will find min value, max value in this list using ‘dart:math’ library
import 'dart:math';
int findMin(List<int> numbers) {
return numbers.reduce(min);
}
int findMax(List<int> numbers) {
return numbers.reduce(max);
}
Let’s do the test
import 'dart:math';
void main() {
final numbers = [4, 2, 8, 3, 1, 5, 7, 9];
print('MIN: ${findMin(numbers)}');
print('MAX: ${findMax(numbers)}');
}
int findMin(List<int> numbers) {
return numbers.reduce(min);
}
int findMax(List<int> numbers) {
return numbers.reduce(max);
}
Output
MIN: 1
MAX: 9