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

135
Visualizações
Extracting common numbers from multi-dimensional array

I'm a little stuck on a JavaScript function that can extract common numbers from a nested array of numbers.

Example input:

[[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

Expected output:

[3, 10]; // because 3 and 10 are present in all 3 arrays

It must work with any arbitrary array of numbers of any size.

Ideas?

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You can do something like this:

var arr = [[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

var result = arr.shift().filter(function(v) {
    return arr.every(function(a) {
        return a.indexOf(v) !== -1;
    });
});

console.log(result);

about 4 years ago · Santiago Gelvez Relatório

0

It might pay to convert the inner arrays into ever shrinking Sets:

const arr = [[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

// Expected output:  [3, 10]; // because 3 and 10 are present in all 3 arrays


let final = new Set(arr[0])

for ( i=1; i < arr.length ; i++) {
  final = new Set (arr[i].filter ( x => final.has(x) ))
}

const newArr = Array.from(final)

console.log(newArr)

about 4 years ago · Santiago Gelvez Relatório

0

var arrays = [[7, 3, 6, 10], [9, 10, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]];

var result = arrays.shift().reduce(function(res, v) {
    if (res.indexOf(v) === -1 && arrays.every(function(a) {
        return a.indexOf(v) !== -1;
    })) res.push(v);
    return res;
}, []);

document.write('<pre>' + JSON.stringify(result,null,4)+ '</pre>');
about 4 years ago · Santiago Gelvez 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