Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda