Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!