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

155
Visualizações
Third Maximum Number in Leetcode-javaScript

Given an integer array nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number. Input: nums = [2,2,3,1] Output: 1

my code:

var thirdMax = function(nums) {
     let tempArr =new Set(nums.sort((a,b) => b-a));
     let newArr = Array.from(tempArr);

  return newArr[2];
  
};

In console it works, but return undefined. May I ask what I'm missing here? Thank you.

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

0

May I ask what I'm missing here?

You are missing this part of the challenge:

If the third maximum does not exist, return the maximum number

Your code is looking for a third entry that might not exist (hence undefined). You'll need to test your code with input that only has 2 or 1 unique value(s), and adapt your code accordingly.

If you can't find how to do that, here is the spoiler:

return newArr[2] ?? newArr[0];

Note that slice expects a numeric argument (not a string). You can just omit that argument.

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