What is the cleanest way to get the sum of numbers in a collection/list in Dart?

IterationDart

Iteration Problem Overview


I don't like using an indexed array for no reason other than I think it looks ugly. Is there a clean way to sum with an anonymous function? Is it possible to do it without using any outside variables?

Iteration Solutions


Solution 1 - Iteration

Dart iterables now have a reduce function (https://code.google.com/p/dart/issues/detail?id=1649), so you can do a sum pithily without defining your own fold function:

var sum = [1, 2, 3].reduce((a, b) => a + b);

Solution 2 - Iteration

int sum = [1, 2, 3].fold(0, (previous, current) => previous + current);

or with shorter variable names to make it take up less room:

int sum = [1, 2, 3].fold(0, (p, c) => p + c);

Solution 3 - Iteration

This is a very old question but

In 2022 there is actually a built-in package.

Just import

import 'package:collection/collection.dart';

and call the .sum extension method on the Iterable.

FULL EXAMPLE

import 'package:collection/collection.dart';

void main() {
  final list = [1, 2, 3, 4];
  final sum = list.sum;
  print(sum); // prints 10
}

If the list is empty, .sum returns 0.

You might also be interested in list.average...

Solution 4 - Iteration

I still think this is cleaner and easier to understand for this particular problem.

num sum = 0;
[1, 2, 3].forEach((num e){sum += e;});
print(sum);

or

num sum = 0;
for (num e in [1,2,3]) {
  sum += e;
}

Solution 5 - Iteration

There is not a clean way to do it using the core libraries as they are now, but if you roll your own foldLeft then there is

main() {
  var sum = foldLeft([1,2,3], 0, (val, entry) => val + entry);
  print(sum);
}

Dynamic foldLeft(Collection collection, Dynamic val, func) {
  collection.forEach((entry) => val = func(val, entry));
  return val;
}

I talked to the Dart team about adding foldLeft to the core collections and I hope it will be there soon.

Solution 6 - Iteration

Starting with Dart 2.6 you can use extensions to define a utility method on the List. This works for numbers (example 1) but also for generic objects (example 2).

extension ListUtils<T> on List<T> {
  num sumBy(num f(T element)) {
    num sum = 0;
    for(var item in this) {
      sum += f(item);
    }
    return sum;
  }
}

Example 1 (sum all the numbers in the list):

var numbers = [1, 2, 3];
var sum = numbers.sumBy((number) => number);

Example 2 (sum all the Point.x fields):

var points = [Point(1, 2), Point(3, 4)];
var sum = points.sumBy((point) => point.x);

Solution 7 - Iteration

I suggest you to create this function in any common utility file.

T sum<T extends num>(T lhs, T rhs) => lhs + rhs;

int, double, float extends num class so you can use that function to sum any numbers.

e.g.,

List<int> a = [1,2,3];
int result = a.reduce(sum);
print(result); // result will be 6

Solution 8 - Iteration

Herewith sharing my Approach:

void main() {
  int value = sumTwo([1, 4, 3, 43]);
  print(value);
}

int sumTwo(List < int > numbers) {
  int sum = 0;

  for (var i in numbers) {
    sum = sum + i;
  }
  return sum;
}

Solution 9 - Iteration

I'd just like to add some small detail to @tmaihoff's answer (about using the collection.dart package):

The sum getter he talks about only works for iterables of num values, like List<int> or Set<double>.

If you have a list of other object types that represent values (like Money, Decimal, Rational, or any others) you must map it to numbers. For example, to count the number of chars in a list of strings you can do:

// Returns 15.
['a', 'ab', 'abc', 'abcd', 'abcde'].map((e) => e.length).sum;

As of 2022, another way of doing it, is using the sumBy() method of the fast_immutable_collections package:

// Returns 15.
['a', 'ab', 'abc', 'abcd', 'abcde'].sumBy((e) => e.length), 15);

Note: I'm the package author.

Solution 10 - Iteration

If you are planning on doing a number of mathematical operations on your list, it may be helpful to create another list type that includes .sum() and other operations by extending ListBase. Parts of this are inspired by this response with performance tweaks from this response.

import 'dart:collection';
import 'dart:core';

class Vector<num> extends ListBase<num> {
  
  List<num> _list;
  Vector() : _list = new List<num>();
  Vector.fromList(List<num> lst): _list = lst;
  
  void set length(int l) {
    this._list.length=l;
  }

  int get length => _list.length;
  
  num operator [](int index) => _list[index];

  void operator []=(int index, num value) {
    _list[index]=value;
  }
  // Though not strictly necessary, for performance reasons
  // you should implement add and addAll.

  void add(num value) => _list.add(value);

  void addAll(Iterable<num> all) => _list.addAll(all);
  
  num sum() => _list.fold(0.0, (a, b) => a + b) as num;
  /// add additional vector functions here like min, max, mean, factorial, normalize etc

}

And use it like so:

Vector vec1 = Vector();
vec1.add(1); 
print(vec1); // => [1]
vec1.addAll([2,3,4,5]); 
print(vec1); // => [1,2,3,4,5]
print(vec1.sum().toString()); // => 15
Vector vec = Vector.fromList([1.0,2.0,3.0,4.0,5.0]); // works for double too.
print(vec.sum().toString()); // => 15

Solution 11 - Iteration

If when using fold gives a double TypeError, you can use reduce:

var sum = [0.0, 4.5, 6.9].reduce((a, b) => a + b);

Solution 12 - Iteration

A solution that has worked cleanly for me is:

var total => [1,2,3,4].fold(0, (e, t) => e + t); // result 10

Solution 13 - Iteration

Different ways to find the sum of all dart list elements,

Method 1: Using a loop : This is the most commonly used method. Iterate through the list using a loop and add all elements of the list to a final sum variable. We are using one for loop here :

main(List<String> args) {
  var sum = 0;
  var given_list = [1, 2, 3, 4, 5];

  for (var i = 0; i < given_list.length; i++) {
    sum += given_list[i];
  }

  print("Sum : ${sum}");
}

Method 2: Using forEach : forEach is another way to iterate through a list. We can also use this method to find out the total sum of all values in a dart list. It is similar to the above method. The only difference is that we don’t have to initialize another variable i and list.length is not required.

main(List<String> args) {
  var sum = 0;
  var given_list = [1, 2, 3, 4, 5];

  given_list.forEach((e) => sum += e);

  print("Sum : ${sum}");
}

Method 3: Using reduce : reduce method combines all elements of a list iteratively to one single value using a given function. We can use this method to find out the sum of all elements as like below :

main(List<String> args) {
  var given_list = [1, 2, 3, 4, 5];

  var sum = given_list.reduce((value, element) => value + element);

  print("Sum : ${sum}");
}

Method 4: Using fold : fold() is similar to reduce. It combines all elements of a list iteratively to one single value using a function. It takes one initial value and calculates the final value based on the previous value.

main(List<String> args) {
  var sum = 0;
  var given_list = [1,2,3,4,5];

  sum = given_list.fold(0, (previous, current) => previous + current);

  print("Sum : ${sum}");
}

for more details:https://www.codevscolor.com/dart-find-sum-list-elements

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
QuestionPhlox MidasView Question on Stackoverflow
Solution 1 - IterationArthurDentureView Answer on Stackoverflow
Solution 2 - IterationUgtemlhrshrwzfView Answer on Stackoverflow
Solution 3 - IterationtmaihoffView Answer on Stackoverflow
Solution 4 - IterationmdakinView Answer on Stackoverflow
Solution 5 - IterationLars TackmannView Answer on Stackoverflow
Solution 6 - IterationvovahostView Answer on Stackoverflow
Solution 7 - Iteration임승기View Answer on Stackoverflow
Solution 8 - IterationramkumarView Answer on Stackoverflow
Solution 9 - IterationMarcGView Answer on Stackoverflow
Solution 10 - IterationRyan DeschampsView Answer on Stackoverflow
Solution 11 - IterationaabiroView Answer on Stackoverflow
Solution 12 - IterationLenin ZapataView Answer on Stackoverflow
Solution 13 - IterationSuresh B BView Answer on Stackoverflow