Dart – Find min, max in a List using dart:math and list reduce()

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

Tagged : / / / / /
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x