const mergeDuplicates = arr => {
const map = arr.reduce((accumulator, currentValue) => {
if (accumulator.has(currentValue)) {
accumulator.set(currentValue, accumulator.get(currentValue) + 1);
} else {
accumulator.set(currentValue, 1);
};
return accumulator;
}, new Map());
return Array.from(map, element => element[0] * element[1]);
};
const arr = [8, 2, 3, 4, 2, 1, 1, 7, 5, 9, 5, 3];
// 8, 2+2=4, 3+3=6, 4, 1+1=2, 7, 5+5=10, 9
console.log(mergeDuplicates(arr));
/*
run:
[
8, 4, 6, 4,
2, 7, 10, 9
]
*/