0

I have an array with 101 values (representing people aged 0-100).

What would be a preferably fast and simple way of building these aggregated arrays in one go:

var input = [55,33,12 .. 98 more]

var output = {   

    //same as input
    i1 = [],

    //0-5, 6-10, 11-15 ... 96-100
    i5 = [],

    //0-10, 11-20, 21-30 ... 91-100
    i10 = [],

    //0-20, 21-40, 41-60 ... 81-100   
    i20 = [],
}

On a side note: Would you name these aggregate arrays by the interval ("i1", "i5") or by the number of groups/elements ("g100", "g20") - what is more intuitive if another programmers comes across these definitions?

dani
  • 4,614
  • 6
  • 47
  • 91
  • You don't need to use an object for output: javascript arrays are sparse: http://stackoverflow.com/questions/1510778/are-javascript-arrays-sparse so output can be an array with just four elements (1, 5, 10, 20). In the end there isn't any difference. And surely by interval would be more intuitive. – xanatos Oct 14 '11 at 14:01
  • @xanatos: But I do use arrays for the actual output? The i1 should have 101 elements; the i5, 20 elements and so on. More specifically I'm interested in the performance of doing this in one loop rather than n separate. (Where n is the number of interval alternatives.) – dani Oct 14 '11 at 14:09
  • It will still be fast! Don't pre-optimize. The only way to do it faster would be to sort the input array and the traverse it by strides of 5, 10 and 20. Otherwhise you'll always have O(n * m) complexity (n = input.length, m = number of intervals) – xanatos Oct 14 '11 at 14:20
  • Are your ranges intentionally of different sizes? The first group always has one more element than the rest of them (e.g. 0-5 has six possible values whereas 6-10, 11-5, ... have only five). – maerics Oct 14 '11 at 14:40

1 Answers1

1

You can reuse results of the aggregation to calculate the next array.

// sums up each n numbers from the input array
//
function groupSum(inarray, n) {
    var outarray = [];
    var sum = 0;
    for (var i = 0; i < inarray.length; i++) {
        sum += inarray[i];
        if (i % n == n - 1) {outarray.push(sum); sum = 0;}
    }
    // add the last element
    if (i % n != 0) { outarray.push(sum); }

    return outarray;
}

var input = [55, 33, 12, 98, /* more numbers here */ 3, 4, 1, 2, 0, 7];

var output = {};
output.i1 = input;
output.i5 = groupSum(output.i1, 5);
output.i10 = groupSum(output.i5, 2);
output.i20 = groupSum(output.i10, 2);

Note that, as xanatos said, performance is not really of big concern here.

PS1: was not sure if you were trying to make output an object (as in this code) or 2D array.

PS2: since your first group always has 1 more element, you might need to adjust the code a bit for this special case.

DK.
  • 2,933
  • 21
  • 33