[Solved] How to compare values of array and if they are equal make them one


You could reduce the array and check if the previous value is equal to the actual value.

function sumSame(arrayA, arrayB) {
    var i,
        l = arrayA.length,
        resultA = [],
        resultB = [];

    for (i = 0; i < l; i++) {
        if (arrayA[i] === arrayA[i - 1]) {
            resultB[resultB.length - 1] += arrayB[i];
        } else {
            resultA.push(arrayA[i]);
            resultB.push(arrayB[i]);
        }
    }
    return [resultA, resultB];
}

console.log(sumSame(
    [1, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4],
    [0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4]
));
.as-console-wrapper { max-height: 100% !important; top: 0; }

3

solved How to compare values of array and if they are equal make them one