Dart List min/max value

Dart

Dart Problem Overview


How do you get the min and max values of a List in Dart.

[1, 2, 3, 4, 5].min //returns 1
[1, 2, 3, 4, 5].max //returns 5

I'm sure I could a) write a short function or b) copy then sort the list and select the last value,

but I'm looking to see if there is a more native solution if there is any.

Dart Solutions


Solution 1 - Dart

Assuming the list is not empty you can use Iterable.reduce :

import 'dart:math';

main(){
  print([1,2,8,6].reduce(max)); // 8
  print([1,2,8,6].reduce(min)); // 1
}

Solution 2 - Dart

If you don't want to import dart: math and still wants to use reduce:

main() {
  List list = [2,8,1,6]; // List should not be empty.
  print(list.reduce((curr, next) => curr > next? curr: next)); // 8 --> Max
  print(list.reduce((curr, next) => curr < next? curr: next)); // 1 --> Min
}

Solution 3 - Dart

You can now achieve this with an extension as of Dart 2.6:

import 'dart:math';

void main() {
  [1, 2, 3, 4, 5].min; // returns 1
  [1, 2, 3, 4, 5].max; // returns 5
}

extension FancyIterable on Iterable<int> {
  int get max => reduce(math.max);

  int get min => reduce(math.min);
}

Solution 4 - Dart

An example to get Min/Max value using reduce based on condition for a list of Map objects

Map studentA = {
  'Name': 'John',
  'Marks': 85
};

Map studentB = {
  'Name': 'Peter',
  'Marks': 70
};

List<Map> students = [studentA, studentB];

// Get student having maximum mark from the list

Map studentWithMaxMarks = students.reduce((a, b) {
    if (a["Marks"] > b["Marks"])
        return a;
    else
        return b;
});


// Get student having minimum mark from the list (one liner)

Map studentWithMinMarks = students.reduce((a, b) => a["Marks"] < b["Marks"] ? a : b);

Another example to get Min/Max value using reduce based on condition for a list of class objects

class Student {
    final String Name;
    final int Marks;

    Student(this.Name, this.Marks);
}

final studentA = Student('John', 85);
final studentB = Student('Peter', 70);

List<Student> students = [studentA, studentB];

// Get student having minimum marks from the list

Student studentWithMinMarks = students.reduce((a, b) => a.Marks < b.Marks ? a : b);

Solution 5 - Dart

If your list is empty, reduce will throw an error.

You can use fold instead of reduce.

// nan compare to any number will return false
final initialValue = number.nan;
// max
values.fold(initialValue, (previousValue, element) => element.value > previousValue ? element.value : previousValue);
// min
values.fold(initialValue, (previousValue, element) => element.value < previousValue ? element.value : previousValue);

It can also use to calculate sum.

final initialValue = 0;
values.fold(initialValue, (previousValue, element) => element.value + previousValue);

Although fold is not cleaner than reduce for getting min/max, it is still a powerful method to do more flexible actions.

Solution 6 - Dart

For empty lists: This will return 0 if list is empty, the max value otherwise.

  List<int> x = [ ];  
  print(x.isEmpty ? 0 : x.reduce(max)); //prints 0

  List<int> x = [1,32,5];  
  print(x.isEmpty ? 0 : x.reduce(max)); //prints 32

Solution 7 - Dart

int minF() {
  final mass = [1, 2, 0, 3, 5];
  mass.sort();
  
  return mass[0];
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbashepsView Question on Stackoverflow
Solution 1 - DartAlexandre ArdhuinView Answer on Stackoverflow
Solution 2 - DartMurali Krishna RegandlaView Answer on Stackoverflow
Solution 3 - DartcreativecreatorormaybenotView Answer on Stackoverflow
Solution 4 - DartAbdul SaleemView Answer on Stackoverflow
Solution 5 - Dart呂學洲View Answer on Stackoverflow
Solution 6 - DartlenzView Answer on Stackoverflow
Solution 7 - DartKleyView Answer on Stackoverflow