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

217
Visualizações
Finds the number present at more than one array w.r.t. performance scale solution javascript

I have a problem statement -

Given Array = [ [1, 1, 5, 2, 3], [4, 5, 6, 4, 3], [9, 4, 4, 1, 5] ], the function should return 4.

Common IDs 1, 3, 4, and 5 are at more than one array. This Finds the number present at more than one array. I am trying to have a better solution for this problem.

let t0 = performance.now();
    
    /* let arr = [
      [1, 2, 2],
      [3, 1, 4]
    ]; */
    
    /* let arr = [
      [4, 3],
      [5, 5],
      [6, 2]
    ]; */
    
    let arr = [
      [1, 1, 5, 2, 3],
      [4, 5, 6, 4, 3],
      [9, 4, 4, 1, 5]
    ]
    
    let allArra = [];
    let finalOutcome;
    
    let checkCOmmon = (arr1, arr2) => {
    
      /*   console.log(arr1);
        console.log(arr2); */
      for (var i = 0; i < arr1.length; i++) {
    
        for (var j = 0; j < arr2.length; j++) {
          if (arr1[i] == arr2[j]) {
            allArra.push(arr1[i]);
          }
        }
      }
    }
    
    
    for (i = 0; i < arr.length; i++) {
      for (j = i + 1; j < arr.length; j++) {
        checkCOmmon(arr[i], arr[j]) // find the common element from both the array 
      }
    }
    
    console.log(allArra)
    
    finalOutcome = allArra.sort().filter(function(item, pos, ary) {
      return !pos || item != ary[pos - 1];
    });
    
    console.log(finalOutcome);
    console.log(finalOutcome.length)
    
    let t1 = performance.now();
    console.log(`time taken ${t1 - t0} milliseconds.`);

My proposed solution - http://jsfiddle.net/neerajswarnkar/6v5omjt0/

How basically we check the performance of function only through profiler I find performance.now() to check the time.

Please help me to have a better solution that will scale.

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

0

One optimized solution could be -

let arr = [
      [1, 1, 5, 2, 3],
      [4, 5, 6, 4, 3],
      [9, 4, 4, 1, 5]
    ];

function solution(A) {
  let len = A.length;
  let hospitals = [];
  for (var i = 0; i < len; i++) {
    for (var j = i + 1; j < len; j++) {
      let conflictShcedule = A[i].filter(schedule => A[j].includes(schedule));
      hospitals.push(...conflictShcedule);
    }
  }

  let uniqScheudle = [...new Set(hospitals)].length;
  return uniqScheudle;
}

console.log(solution(arr));

let me know your thoughts..

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