I am making a function that eliminates the repeated numbers of an array (eliminates them completely and only returns the one that is not repeated). This is my function that is doing the opposite. For the example of [1, 2, 1 ] returns [1] :
function noRepetido(numeros) {
const numero = numeros.filter((numero, i, self) => (
self.indexOf(numero) != i
))
return numero;
}
console.log(noRepetido([1, 2, 1]));
If instead of using the distinct operator !=
I use the equals, ===
it returns the array without repeating any number [1,2] for the example.
Excuse me, in the morning they already gave you a similar answer, you just had to adapt it:
Credits to its author, then I'll give you a reward.
Remove repeated characters in a string with javascript
You can use it like this:
You can fix it by looping through the array just once, like this:
eg: checkeds
) where you register the numbers already controlled.Example:
You can make use of Set and remove the repeated elements.