Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Remove array duplicates (the old way)

What's up guys, I was just messing arround LeetCode, and different ways to solving this problem (I know the Set way and the for of with array.includes + push, and the filter, but all of them create a new array), and with this method I'm getting this output, can someone explain me why?

P.S. You can't create a new array, just modify the first one.

let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];

var removeDuplicates = (nums) => {
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] === nums[i + 1]) {
      nums.splice(i, i + 1);
    }
  }
};

removeDuplicates(nums);

console.log(nums);
// [0, 1, 3, 4]

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As you say, the job of the splice method is to modify the contents of the array. The two args you're passing it are the start position and the delete count. You also need the replacement value, and to keep replacing until that condition is not longer true, so the while loop is also an option:

let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];

var removeDuplicates = (nums) => {
  for (let i = 0; i < nums.length; i++) {
    while (nums[i] === nums[i + 1]) {
      nums.splice(i, 2, nums[i]);
    }
  }
};

removeDuplicates(nums);

console.log(nums);

Output:

[ 0, 1, 2, 3, 4 ]
about 4 years ago · Santiago Trujillo Relatório

0

The first problem is here: nums.splice(i, i + 1);

Array splice method takes 2 arguments, the first one is the index of element, and the second one is a number of elements to replace/remove (it seems you confused the second argument with the end index). Source: Array.prototype.splice()

Another problem is changing the original array, while iterating over it (be very careful with such in-place manipulations, they very often result in some kind of errors). Look at what is happening in your loop:

  1. You have some array [0, 0, 0, 1, 1, 2, 2];
  2. You want to remove the first element, since it is a duplicate: [0, 0, 0, 1, 1, 2, 2];
  3. After removing, you have [0, 0, 1, 1, 2, 2];
  4. You increment your i, so now it is equal to 1;
  5. You look at the element with index 1, and find no duplicates [0, 0, 1, 1, 2, 2], since you just skipped the element, that moved into the first index in place of the removed element.

The solution is to decrement the i after removing the element:

let nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];

var removeDuplicates = (nums) => {
  for (let i = 0; i < nums.length; i++) {
    if (nums[i] === nums[i + 1]) {
      nums.splice(i, 1);
      i--;
    }
  }
};

removeDuplicates(nums);

console.log(nums);
// [0, 1, 2, 3, 4]

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda