5 different ways to find the sum of all dart list elements

Introduction :

Lists are iterable in Dart. Lists are categorized into two kinds: growable and fixed-length lists. We can change the size of a growable list and the size is fixed for fixed-length lists. Sometimes, we need to find the total sum of all elements of an integer list. We can easily do that by iterating through each element of the list.

But Dart provides a couple of methods to find out the sum of all the list elements without using a loop. In this post, I will show you 4 different ways to find out the sum of all elements of a list.

Method 1: By 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 in this example:

main() {
  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}");
}

Download it from Github

  • In this example, the for loop starts from i = 0 and on each iteration, it increases the value of i by 1.
  • It will stop once the value of i is equal to the last element’s index i.e. length of the list - 1.
  • On each step of the loop, it adds the value at index i to the sum variable. The sum variable is initialized as 0 at the start of the program.
  • At the end of the program, it prints the sum variable, which is the sum of the elements of the list given_list.

You can also use any other loop. For example, the following program uses a while loop:

main() {
  var sum = 0;
  var given_list = [1, 2, 3, 4, 5];
  var i = 0;

  while (i < given_list.length) {
    sum += given_list[i];
    i++;
  }

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

Download it from Github

Both of these programs will print the same output:

Sum: 15

Method 2: By using the forEach method:

The forEach method provides 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 examples. The only difference is that we don’t have to initialize another variable i and we don’t need to find the length of the list.

main() {
  var sum = 0;
  var given_list = [1, 2, 3, 4, 5];

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

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

Download it from Github

Much simpler than using a for or while loop. It will print the same output.

Method 3: By using the reduce method:

The reduce method combines the 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() {
  var given_list = [1, 2, 3, 4, 5];

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

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

Note that it will fail if the list is empty. We need to handle it for empty lists:

main() {
  var given_list = [1, 2, 3, 4, 5];

  var sum = given_list.length > 0
      ? given_list.reduce((value, element) => value + element)
      : 0;

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

Download it from Github

Method 4: By using the fold method :

The fold() method 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() {
  var sum = 0;
  var given_list = [1, 2, 3, 4, 5];

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

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

Download it from Github

The fold method also works for an empty list.

Combining all methods:

The following example shows how it works with different methods:

main() {
  // method 1
  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 (for loop): ${sum}");

  // method 2
  var i = 0;
  sum = 0;
  while (i < given_list.length) {
    sum += given_list[i];
    i++;
  }

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

  // method 3
  sum = 0;
  given_list.forEach((e) => sum += e);
  print("Sum (forEach): ${sum}");

  // method 4
  sum = given_list.length > 0
      ? given_list.reduce((value, element) => value + element)
      : 0;
  print("Sum (reduce): ${sum}");

  // method 5
  sum = given_list.fold(0, (previous, current) => previous + current);
  print("Sum (fold): ${sum}");
}

Download it from Github

Dart find list elements

You might also like: