I try to remove elements in an array if the number of contiguous element is greater than two.
Here are the tests:
test('delete pieces if number of piece contiguous greater than two', t => {
t.deepEqual([null, null, null], removeIdenticalPieces([1, 1, 1]));
t.deepEqual([null, null, null, null], removeIdenticalPieces([1, 1, 1, 1]));
t.deepEqual([null, null, null, null, 4], removeIdenticalPieces([1, 1, 1, 1, 4]));
t.deepEqual([4, null, null, null, null], removeIdenticalPieces([4, 1, 1, 1, 1]));
t.deepEqual([1, 1, 4, 1, 1], removeIdenticalPieces([1, 1, 4, 1, 1]));
t.deepEqual([1, 1], removeIdenticalPieces([1, 1]));
});
Here is a working function:
function removeIdenticalPieces(line) {
const newLine = [];
while (line.length !== 0) {
const firstPiece = line.shift();
let nbContiguousPieces = 0;
for (let i = 0; i < line.length; i++) {
let currentPiece = line[i];
if (firstPiece !== currentPiece) {
break;
}
nbContiguousPieces += 1
}
if (nbContiguousPieces >= 2) {
newLine.push(null);
for (let j = 0; j < nbContiguousPieces; j++) {
line.shift();
newLine.push(null)
}
} else {
newLine.push(firstPiece);
for (let k = 0; k < nbContiguousPieces; k++) {
newLine.push(line.shift())
}
}
}
return newLine;
}
Does a more "functional" way exist to do the same?
Edit: thank you for your solutions.
Here the jsperf https://jsperf.com/removeidenticalpieces3.
I take the solution of @lilobase because it is faster and more readable.