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

110
Visualizações
Array data structure

I strucked at the line 11th line of the code , can we return the code insted of doing that , i am getting confused.

 var findPairs = function(nums, k) {
        let prev = {} , pairs = 0;
        nums.sort((a,b)=>a-b);
        for(var i=0;i<nums.length;i++){
            let key = nums[i];
            if(!prev[key]){
                let diff = key - k;
                if(prev[diff]!==undefined){
                    pairs++;
                }
                prev[key] = 1;
              }else if(k===0){
                  if(prev[key]===1){
                      pairs++
                  }
                  
                  prev[key]++;
              }
            }
        return pairs;
    };
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

I made a lot of assumptions, but if my assumptions are correct you are asking why we need else if statement. I assume that this function searches for the pairs in array that have difference k. If this is correct, then the else if case is needed when k is 0 and in array you have same element multiple times. For example:

If you have k=0 and nums=[3, 4, 5, 3], we have to count 3 and 3 since they are the pairs too.

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