How to find the sum of an array of numbers
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Summing an array of numbers is one of the most fundamental operations in programming. Every language provides at least one built-in way to do it, and most offer several options ranging from simple loops to functional-style reduce operations. The choice depends on readability, performance, and whether you need to handle edge cases like empty arrays, floating-point precision, or very large numbers. This article covers the standard approaches in JavaScript, Python, Java, C#, Go, and Rust.
JavaScript
Always provide an initial value (0) to reduce(). Without it, an empty array throws a TypeError.
Python
Python's sum() is the idiomatic choice. Use math.fsum() when floating-point precision matters and numpy.sum() for large numerical arrays.
Java
Arrays.stream(arr).sum() is the clean modern approach. Use .asLongStream() if the sum could exceed Integer.MAX_VALUE (approximately 2.1 billion).
C#
LINQ's .Sum() is the standard approach in C#. For high-performance scenarios, a foreach loop over a Span<T> avoids the LINQ overhead.
Go
Go does not have a built-in sum() function or a reduce method. A for range loop is the standard and only approach.
Rust
Rust's .iter().sum() requires a type annotation because Rust needs to know which numeric type to use for accumulation.
TypeScript with Type Safety
Performance Comparison (JavaScript)
For most applications, the performance difference is negligible. Use reduce for readability. Use a for loop in performance-critical hot paths.
Common Pitfalls
- Empty array without initial value in reduce:
[].reduce((a, b) => a + b)throws an error in JavaScript. Always provide0as the initial value to handle empty arrays gracefully. - Integer overflow: In Java (
int), C (int), and similar typed languages, summing large arrays can overflow. UselongorBigIntegerwhen the sum could exceed the integer range. - Floating-point precision:
sum([0.1, 0.2, 0.3])may not equal0.6exactly due to IEEE 754 representation. Usemath.fsum()in Python or Kahan summation in other languages for precise floating-point sums. - Shadowing Python's built-in sum: Naming a variable
sum(sum = 0) overwrites the built-insum()function for the rest of the scope. Usetotalinstead. - NaN propagation: In JavaScript, if the array contains
NaN,undefined, or non-numeric values, the sum becomesNaN. Filter or validate the array before summing:arr.filter(Number.isFinite).reduce(...).
Summary
- Python:
sum(array)— simplest built-in, usemath.fsumfor float precision - JavaScript:
array.reduce((a, b) => a + b, 0)— always include the initial value0 - Java:
Arrays.stream(arr).sum()— use.asLongStream()for large sums - C#:
array.Sum()via LINQ - Go:
for rangeloop (no built-in sum function) - Rust:
array.iter().sum()with type annotation - For all languages, handle empty arrays and integer overflow as edge cases
Related reading
- How to find the total number of Increasing sub-sequences of certain length with Binary Index TreeBIT
- How to find the winner of a tic-tac-toe game of any size?
- How to find two most distant points?
- How to find what is the rank of each element in an integer array
- How to find validity of a string of parentheses, curly brackets and square brackets?
- How to find whether the shortest path from s any starting vertex to v any vertex in the undirected graph is unique or not?
- How to finding first common ancestor of a node in a binary tree?
- how to fix slow kmeans of opencv

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.