Dart is an open-source programming language developed by Google. Finding the sum of a list is essential in various applications, from calculating total costs in financial applications to determining aggregate data in analytics, making it a fundamental operation in data processing and manipulation tasks.
We can calculate the sum of a list of numbers by using a loop and maintaining a variable to accumulate the sum of all elements in the list. We can also use the reduce
method from the dart:core
library, or use a recursive approach to solve the problem.
We can use a very basic iterative approach to solve this problem. In the code below, we defined the sumUsingLoop
funtion that takes a list of integers numbers
and returns an integer (int
) as the sum
.
int sumUsingLoop(List<int> numbers) {int sum = 0;for (int number in numbers) {sum += number;}return sum;}void main() {List<int> numbers = [1, 2, 3, 4, 5];int result = sumUsingLoop(numbers);print('Sum of numbers: $result'); // Output: Sum of numbers: 15}
Line 2: This line initializes a variable named sum
and sets its value to 0
. This variable will store the sum of the elements in the input numbers
slice.
Line 3: A for
loop that iterates over each element in the numbers
list. During each iteration, the value of the current element is assigned to the variable number
, and the value of the current element (number
) is added to the sum
variable.
Line 6: The function returns the final value of the sum
variable, which represents the sum of all elements in the input numbers
slice.
reduce
methodIn Dart, the reduce
method is available on an iterable (such as a list) and allows us to combine elements in the list. It takes a callback function as an argument, which is applied to each element in the list, reducing the list to a single value. In this case, we use the reduce
method to calculate the sum of all elements in the input list of numbers.
int sumUsingReduce(List<int> numbers) {return numbers.reduce((value, element) => value + element);}void main() {List<int> numbers = [1, 2, 3, 4, 5];int result = sumUsingReduce(numbers);print('Sum of numbers: $result'); // Output: Sum of numbers: 15}
Line 1: The sumUsingReduce
function takes a list of integers (numbers
) as input.
Line 2: The reduce
method iterates through each list element, cumulatively adding them to calculate the final sum. The function returns this sum as the result.
Lines 6–8: In the main
function, we create a list of numbers [1, 2, 3, 4, 5]
. Then call the sumUsingReduce
function with this list, storing the result in the variable result
. Finally, we print the sum of the numbers to the console.
In the code below, the sumListRecursive
function calculates the sum of a list of integers using recursion.
int sumListRecursive(List<int> numbers) {if (numbers.isEmpty) {return 0;}return numbers.first + sumListRecursive(numbers.sublist(1));}void main() {List<int> numbers = [1, 2, 3, 4, 5];int result = sumListRecursive(numbers);print('Sum of numbers: $result'); // Output: Sum of numbers: 15}
Line 1: This line defines a function named the sumListRecursive
that takes a list of integers (numbers
) as its input parameter. The function returns an integer, the sum of all elements in the input list.
Lines 2–4: This line checks whether the input list numbers
is empty using the isEmpty
property. If the list is empty, there are no elements to sum. In that case, the function returns 0
as the sum (base case of the recursion).
Line 5: This line is the recursive step of the function.
numbers.first
returns the first element of the list numbers
. This element is added to the sum of the rest of the list.
numbers.sublist(1)
returns a new list that contains all elements of the original list numbers
, except for the first element. In other words, it returns a list with all elements from index 1 to the end of the list.
All three methods have a time complexity of reduce
method have efficient space complexity, using only a fixed amount of additional space
Iterative | Reduce | Recursive | |
Time complexity | O(n) | O(n) | O(n) |
Space complexity | O(1) | O(1) | O(n) |
We can choose the best method for our coding style and performance needs. Both the iterative and reduce
methods are more space-efficient, while the recursive approach offers an alternative way to solve the problem using a different approach.
Free Resources