Interview Questions

Sum of several arrays
The questions is as follows:
You will be given an array of several arrays that each contain integers and your goal is to write a function that will sum up all the numbers in all the arrays. For example, if the input is [[3, 2], [1], [4, 12]] then your program should output 22 because 3 + 2 + 1 + 4 + 12 = 22.

## Solution

We will solve this challenge by looping through the entire array, and then looping through each inner array adding up all the numbers.

## Code

```function sum_array(arr) {

var sum = 0;

// loop through entire array
for (var i = 0; i < arr.length; i++) {

// loop through each inner array
for (var j = 0; j < arr[i].length; j++) {

// add this number to the current final sum
sum += arr[i][j];

}

}

return sum;

}

sum_array([[3, 2], [1], [4, 12]]);
```
```def sum_array(arr):

final_sum = 0

# loop through entire array
for i in range(0, len(arr)):

# loop through each inner array
for j in range(0, len(arr[i])):

# add this number to the current final sum
final_sum += arr[i][j]

return final_sum

print sum_array([[3, 2], [1], [4, 12]])
```
mrdaniel published this on 12/14/15 |
• +
• 11
• -
• With functional programming: ```function sumArray(arr) { return arr.reduce((t, e) => t.concat(e)).reduce((t, e) => t + e) }```
• +
• 3
• -
• ``` function arraySum(arr) { let sum = 0; arr.map((item) => { if (Array.isArray(item)) { sum += arraySum(item); } else { sum += item; } }); return sum; } console.log(arraySum([[3, 2], [1], [4, 12]])); ```
• +
• 1
• -
• ``` function sumNestedArrays(array) { var flattenedArray = []; array.forEach(function(element) { if (Array.isArray(element)) flattenedArray.push(sumNestedArrays(element)); else flattenedArray.push(element); }); return flattenedArray.reduce(function(a,b) { return a+b; }); } ```
• +
• 1
• -
• ``` function addArray(arr){ var count = 0; for(var i=0; i<arr.length; i++){ count += arr[i].reduce(function(c, p){return c + p}); } return count; }; addArray([[3, 2], [1], [4, 12]]); ```
• +
• 1
• -
• The recursion worked great for nested array. ``` def sum_array(lst): if type(lst) == int: return lst sum = 0 for e in lst: if type(e) == list: sum = sum+sum_array(e) else: sum = sum+e return sum assert sum_array([1,[2,3],[4,[5,[6,7]]]]) == 28 ```
• +
• 0
• -
• ``` my_list = [[3, 2], [1], [4, 12]] my_sum = 0 for sub_list in my_list: for item in sub_list: my_sum = my_sum + item print my_sum ```
• +
• 0
• -
• ``` function sum_array(arr) { return arr.reduce(function (a, b) { return a.concat(b); }). reduce(function (a, b) { return a + b; }); } ```
• +
• 0
• -
• This solution only works with 1 "nested layer" of arrays. In case you're asked to find a solution for n "nested layers"(like I was recently..) I suggest you use recursion and Python's "type" method(I know how to do this in Ruby as well, but not sure about JS) Something like this: ``` def my_function(arr): my_sum = 0 for elm in arr: if type(elm) == int: my_sum += elm elif type(elm) == list: temp = my_function(elm) my_sum += temp return my_sum my_list = [1,[2,[3,4]],5] print my_function(my_list) ```
• +
• 0
• -
• ```function sumItUp(arr) { var result = 0; arr.forEach(function(ele, i) { if(Array.isArray(ele)){ ele.forEach(function(el, i) { result+=el; }); }else { result+= ele; } }); return result; }```
• +
• 0
• -
• //@ shaunw throw's an exception ``` function multi_array_sum (arr){ var result = arr.reduce(function (a, b) { return a.concat(b);},[]) return result.reduce(function(a,b) { return a+b;} ); }``` // if this can be pared further have at it everyone
• +
• 0
• -
• ```def sum_array(arr):
return sum(sum(arr, []))
```
The most easier way I know.
• +
• 0
• -
• ```Def sum_array(are):
return sum(sum(are, []))

```

• +
• 0
• -
• ```const sumAllArr = (arr) => { let accum = 0 arr.map((num) => { accum += num.reduce((a, b)=> { return a + b }) }) return accum } sumAllArr([[3, 2], [1], [4, 12]])```
• +
• 0
• -
• More readable example with the possibility to sum the numbers on the nested arrays on many levels. ``` function SumNestedArrays(arr) { return arr.reduce(function (flat, toFlatten) { return flat.concat(Array.isArray(toFlatten) ? SumNestedArrays(toFlatten) : toFlatten); }, []).reduce( (a,b) => a + b, 0); } const input = [[2, 2], [1,[2,1]], [2, [2,[1,[1]]], 1]] ; const sum = SumNestedArrays(input); ```
• +
• 0
• -
• ``` function sumArray(arr) { var result = arr.reduce((prev, cur) => { return prev.concat(cur); }); return result.reduce((p, c) => { return p + c; }); } sumArray([[5,4],[3,2], [1,0]]); ```