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

269
Visualizações
Get the index of duplicate values except the first

Below is an example array:

const attD = [1, 2, 3, 4, 3, 3]

How can we get the index of the duplicate values from this array? In this example, 3 is repeated thrice. So, I want to get the index 4, index 5 as these are duplicated at that positions (ignoring the first occurrence).

I have tried the below code, but it is giving me values. Instead, I need the index.

const findDuplicates = attD => attD.filter((item, index) => attD.indexOf(item) !== index);
const duplicates = findDuplicates(attD);
console.log(duplicates);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  1. With .map(), add the index of duplicated values into the array. For non-duplicate value, will be added with undefined value [Will be filtered in Step 2].
  2. With .filter(), remove the value which is undefined from the array.

const attD = [1, 2, 3, 4, 3, 3];

const findDuplicates = attD => attD.map((item, index) => {
  if (attD.indexOf(item) !== index)
    return index;
}).filter(x => x);  // Equivalent to .filter(x => x != undefined || x != null)

const duplicates = findDuplicates(attD);
console.log(duplicates);

about 4 years ago · Juan Pablo Isaza 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