Calculating the average for each subarray separately and then return the sum of all the averages in JavaScript

In JavaScript, when working with arrays of subarrays, we often need to calculate the average for each subarray separately and then return the sum of all these averages. This can be efficiently accomplished using JavaScript's built-in array methods like reduce().

What is the reduce() Method in JavaScript?

The reduce() method in JavaScript reduces an array to a single value by iterating over each element and applying a callback function that accumulates a value based on each iteration. It takes two main arguments: an accumulator (the accumulated value from previous iterations) and the current value being processed.

const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((acc, val) => acc + val, 0);
console.log(sum);
15

Algorithm for Sum of Averages

To solve this problem, we need to:

  1. Iterate through each subarray
  2. Calculate the sum of elements in each subarray using reduce()
  3. Divide by the subarray length to get the average
  4. Add each average to our running total
  5. Return the final sum

Implementation Using For Loop

function sumOfAverages(arr) { 
  let sum = 0; 
  for (let subArr of arr) {
    const avg = subArr.reduce((acc, val) => acc + val, 0) / subArr.length;
    sum += avg;
  }
  return sum;
}

const arr = [[1, 2, 3], [4, 5], [6, 7, 8, 9]];
const result = sumOfAverages(arr);
console.log("Sum of averages:", result);
Sum of averages: 14

Alternative Implementation Using reduce()

We can also solve this problem using reduce() for the outer iteration:

function sumOfAveragesReduce(arr) {
  return arr.reduce((sum, subArr) => {
    const avg = subArr.reduce((acc, val) => acc + val, 0) / subArr.length;
    return sum + avg;
  }, 0);
}

const arr = [[1, 2, 3], [4, 5], [6, 7, 8, 9]];
const result = sumOfAveragesReduce(arr);
console.log("Sum of averages (reduce):", result);
Sum of averages (reduce): 14

Step-by-Step Calculation

Let's trace through the calculation for [[1, 2, 3], [4, 5], [6, 7, 8, 9]]:

const arr = [[1, 2, 3], [4, 5], [6, 7, 8, 9]];

// Subarray 1: [1, 2, 3]
// Sum = 1 + 2 + 3 = 6, Average = 6/3 = 2

// Subarray 2: [4, 5]  
// Sum = 4 + 5 = 9, Average = 9/2 = 4.5

// Subarray 3: [6, 7, 8, 9]
// Sum = 6 + 7 + 8 + 9 = 30, Average = 30/4 = 7.5

// Final sum = 2 + 4.5 + 7.5 = 14

console.log("Average of [1,2,3]:", (1+2+3)/3);
console.log("Average of [4,5]:", (4+5)/2);
console.log("Average of [6,7,8,9]:", (6+7+8+9)/4);
console.log("Sum of all averages:", 2 + 4.5 + 7.5);
Average of [1,2,3]: 2
Average of [4,5]: 4.5
Average of [6,7,8,9]: 7.5
Sum of all averages: 14

Complexity Analysis

Time Complexity: O(n×m) where n is the number of subarrays and m is the average length of each subarray. We iterate through each subarray once, and for each subarray, we use reduce() which takes O(m) time.

Space Complexity: O(1) as we only use a constant amount of extra memory to store the sum and average values.

Conclusion

The reduce() method provides an elegant solution for calculating the sum of subarray averages in JavaScript. Both the for-loop and reduce-based implementations achieve the same result with O(n×m) time complexity, making them suitable for moderate-sized datasets.

Updated on: 2026-03-15T23:19:00+05:30

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements