当前位置:网站首页>414. The third largest digital buckle

414. The third largest digital buckle

2022-07-06 22:04:00 Big chicken legs are best

Give you a non empty array , Returns the value in this array The third largest number . If it doesn't exist , Then return the maximum number in the array .

Example 1:

Input :[3, 2, 1]
Output :1
explain : The third largest number is 1 .
Example 2:

Input :[1, 2]
Output :2
explain : The third largest number doesn't exist , So return the maximum number 2 .
Example 3:

Input :[2, 2, 3, 1]
Output :1
explain : Be careful , Ask to return the third largest number , It's the third largest of all the different numbers .
There are two values in this example that are 2 Number of numbers , They're both second . The third largest of all the different numbers is 1 .

source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/third-maximum-number
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

###  Their thinking

Here is the solution

Sort from

###  Code

```javascript

/**

 * @param {number[]} nums

 * @return {number}

 */

var thirdMax = function(nums) {

   nums=[...new Set(nums.sort((a,b)=>{return a-b}))]

   let len=nums.length

   if(len<3)return nums[len-1]

   return nums[len-3]

};

```

原网站

版权声明
本文为[Big chicken legs are best]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131107090484.html