Wednesday, December 17, 2014

Counting occurences of Javascript array elements

var arr = [2, 2, 2, 2, 2, 4, 5, 5, 5, 9];

function foo(arr) {
    var a = [], b = [], prev;
   
    arr.sort();
    for ( var i = 0; i < arr.length; i++ ) {
        if ( arr[i] !== prev ) {
            a.push(arr[i]);
            b.push(1);
        } else {
            b[b.length-1]++;
        }
        prev = arr[i];
    }
   
    return [a, b];
}

var result = foo(arr);
document.write('[' + result[0] + ']<br>[' + result[1] + ']')

[2,4,5,9]
[5,1,3,1]

No comments:

Post a Comment