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

126
Visualizações
Return duplicate objects in array of large data using JavaScript

Using JavaScript: I have an array of objects that I'm trying to determine the duplicate entries of so that I can eventually pass those duplicate entries to a separate function to remove them from a database.

My sample array could be:

const myArray = [
    { 'id': 111, 'lorem': 'ipsum' },
    { 'id': 222, 'lorem': 'dorem' },
    { 'id': 111, 'lorem': 'polus' },
    { 'id': 111, 'lorem': 'waifu' },
]

I'd want to return an array of all items that would be duplicate by the key id. In this example, my returned array would be:

[
    { 'id': 111, 'lorem': 'ipsum' },
    { 'id': 111, 'lorem': 'polus' },
    { 'id': 111, 'lorem': 'waifu' },
]

Most of the online tutorials have me iterating over a short list of data, and is great for such small data examples. But my dataset is in the thousands, if not millions, as my data grows. So I'm trying to find a smarter way of handling this logic.

I understand that I can run a Set(), but that doesn't actually give me the duplicate entries - that gives me an array with non-duplicates. My need is to return such duplicates, not to have a new array of non-duplicate entries.

Without using a third party such as lodash or underscore, how would I ideally iterate over an array with unknown size, to eventually return the duplicate items for me to pass up the stream for processing?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

  • Using Array#reduce, iterate over the array while updating a Map to group items by id
  • Using Map#values, get the list of grouped arrays
  • Using Array#filter, keep the arrays with more than one item
  • Using Array#flat, return all arrays in one list

const myArray = [ { 'id': 111, 'lorem': 'ipsum' }, { 'id': 222, 'lorem': 'dorem' }, { 'id': 111, 'lorem': 'polus' }, { 'id': 111, 'lorem': 'waifu' } ];

const duplicates = 
  [...myArray.reduce((map, item) => // group items by id
    map.set(item.id, [...(map.get(item.id) || []), item])
  , new Map)
  .values()] // get grouped arrays
  .filter(list => list.length > 1) // keep duplicates
  .flat(); // return one array

console.log(duplicates);

about 4 years ago · Juan Pablo Isaza Relatório

0

This may take a while

I recommend you do this on the server

// create an array with random IDs
const myArray = []
for (let i = 0; i < 10000; i++) {
  myArray.push({
    id: String(Math.floor(Math.random() * 10000)).padStart(3, "0"),
    "lorem": "ipsum"
  })
}

// examine them
const ids = []
const dupes = []
myArray.forEach(({id}) => {
  if (ids.includes(id)) dupes.push(id);
  else ids.push(id)
})
console.log(ids.length, dupes, dupes.length)

about 4 years ago · Juan Pablo Isaza Relatório

0

Expanding on my comment:

const myArray = [
    { 'id': 111, 'lorem': 'ipsum' },
    { 'id': 222, 'lorem': 'dorem' },
    { 'id': 111, 'lorem': 'polus' },
    { 'id': 111, 'lorem': 'waifu' },
];
const uniqueItems = new Set();
const duplicates = [];
myArray.forEach(
  function(a) {
    if (uniqueItems.has(a.id)) {
      duplicates.push(a);
    }
    uniqueItems.add(a.id);
   }
);
console.log("duplicates",duplicates);
console.log("uniqueItems",uniqueItems);

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