So for ex. I have this array with these values :
let arr = [1, 2, 3, 3, 3, 4, 4];
How can I get a new arr as :
let newArr = [3, 3, 4, 4];
while at the same time the original array should be changed as :
let arr = [1, 2, 3];
one 3 is left behind cause there is only one pair of 3.
I tried this code below ( a for loop & splice ) but it is not working properly.
let result = [];
for (let i = 0; i < sorted.length; i++) {
if (sorted[i] === sorted[i + 1]) {
let pair = sorted.splice(i, 1);
pair.forEach(el => result.push(el));
}
}