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

210
Visualizações
While joining two arrays, postfix non-unique values with (number) incrementally of joining array

I have two arrays:

  • Array 1 : [{name: "Darin", id: "123"}, {name: "Mads", id: "345"}, {name: "Kenneth", id: "543"}, {name: "June", id: "567"}, {name: "June (1)", id: "789"}]
  • Array 2 : [{name: "Darin", id: "910"}, {name: "June", id: "911"}, {name: "Simon", id: "912"}, {name: "Justin", id: "913"}]

The output that I want is as follows:

[{name: "Darin", id: "123"}, {name: "Mads", id: "345"}, {name: "Kenneth", id: "543"}, {name: "June", id: "567"}, {name: "June (1)", id: "789"}, {name: "Darin (1)", id: "910"}, {name: "June (2)", id: "911"}, {name: "Simon", id: "912"}, {name: "Justin", id: "913"}]

As you can see, I have the following rules:

  • Array 2 should be the only array in which I postfix (...num)
  • Eventual duplicates should be postfixed with (number) where number increments until the value becomes unique in the joined array

I'm unsure where to start in order to achieve the above. I can see this solved with a bunch of loops and a recusive function - But really, that's inefficient.

I can see that Javascript ES6 offers some "newer" methods that could pose handy in this scenario - In particular map and set.

Consequently, my question seeks to understand how I can achieve the above, while paying attention to time complexity.

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

0

You could count the names by separating the strings.

const
    array1 = ["Darin", "Mads", "Kenneth", "June", "June (1)"],
    array2 = ["Darin", "June", "Simon", "Justin"],
    counts = {},
    result = [array1, array2].reduce((r, a) => {
        a.forEach(v => {
            const [, name, , count = 0] = v.match(/^(\w*)(\s\((\d+)\))?$/);
            if (name in counts && count === 0) ++counts[name];
            else counts[name] = count;
            r.push(counts[name] ? `${name} (${counts[name]})` : name);
        });
        return r;
    }, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Using Array.map, and a counter object to keep track of the number of times each name already occurred:

var input1 = [{name: "Darin", id: "123"}, {name: "Mads", id: "345"}, {name: "Kenneth", id: "543"}, {name: "June", id: "567"}, {name: "June (1)", id: "789"}];

var input2 = [{name: "Darin", id: "910"}, {name: "June", id: "911"}, {name: "Simon", id: "912"}, {name: "Justin", id: "913"}];

var counter = {};
var combined = input1.concat(input2).map(function(e) {
  var strippedName = e.name.replace(/ \([0-9]+\)/, '');
  counter[strippedName] = counter[strippedName] == undefined ? 0 : counter[strippedName] + 1;
  e.name = strippedName + (counter[strippedName] ? ' ('+counter[strippedName]+')' : '');
  return e;
});
console.log(combined);

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