Find average of each array within an array in JavaScript


In the given problem statement we are presented with an array which contains multiple subarrays. So our aim is to compute the average of every individual subarray and store these averages in a new array. And implement the solution in Javascript.

Understanding the problem

The problem has given an array which contains multiple arrays or nested arrays. Every subarray contains some numerical values. Our task is to compute the average of every subarray and collect these averages into a new array. For example we have arrays like this [[4, 6], [2, 4], [6, 2]] so the average of first subarray 4 and 6 is 5, average of second array 2 and 4 is 3, and average of third array 6 and 2 is 4. So the result array of averages of the subarrays is [5, 3, 4].

Logic for the given problem

To implement the code we will iterate through the main array and we will access each array chunk each at a time. Inside each subarray we will calculate the sum of its own items and divide that sum by the length of the subarray to get the average value. This average value then will be added to the new array. After implementing the solution we will be able to determine the average value for every subarray inside the main array.

Algorithm

Step 1: Define a function to get the average of subarrays of the input array. And give it a name as calAvgOfArr.

Step 2: We need the average values of the subarrays, so for that we need an array to store the average values of the subarrays and give it a name as 'averages'. Which is declared as an empty object.

Step 3: For loop is used to traverse the outer array till its length property using the i variable.

Step 4: Inside the above loop, we have to keep track of the sum of items in inner arrays so define a variable called sum to store the sum values.

Step 5: Inside the first loop we use another loop to traverse the inner array items with the j variable and add it to the sum.

Step 6: The average values will be calculated by dividing the sum by the length of the current subarray or inner array.

Step 7: Once all the loops are over, and after getting the average values of each inner arrays insert the values in the array we have created in the second step.

Example

//Function to calculate the average of subarrays
function calAvgOfArr(arr) {
   // Array to store the result array
   const averages = [];

   for (let i = 0; i < arr.length; i++) {
      let sum = 0;
      for (let j = 0; j < arr[i].length; j++) {
         sum += arr[i][j];
      }

      const average = sum / arr[i].length;
      averages.push(average);
   }

   return averages;
}
const array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
const result = calAvgOfArr(array);
console.log(result);

Output

[ 2, 5, 8 ]

Complexity

The following problem statement is solved using javascript methods like for loops and push method that traverses the array of elements in O(n * m) worst case time complexity. The space complexity of the problem statement is O(m).

Conclusion

With the usage of nested for loop we have easily calculated the average value of each subarray in Javascript. The complexity for the algorithm is O(n * m). This means it can handle arrays of moderate size effectively. But if the array size becomes very large then the code may take longer than expected time to execute.

Updated on: 14-Aug-2023

403 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements