median method

double median()

Returns the median of the elements in this collection.

Empty collections throw an error. null values are ignored.

Implementation

double median() {
  if (length == 0) throw StateError('No elements in collection');
  final values = toList()
    ..removeWhere((item) => item == null)
    ..sort();
  final size = values.length;
  if (size.isOdd) {
    return values[(size / 2).floor()].toDouble();
  } else {
    final x = values[(size / 2).floor()];
    final y = values[(size / 2).floor() - 1];
    return (x + y) / 2;
  }
}