Skip to content

Commit 18e53c6

Browse files
committed
Add solution #414
1 parent 0729718 commit 18e53c6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
374|[Guess Number Higher or Lower](./0374-guess-number-higher-or-lower.js)|Medium|
122122
383|[Ransom Note](./0383-ransom-note.js)|Easy|
123123
387|[First Unique Character in a String](./0387-first-unique-character-in-a-string.js)|Easy|
124+
414|[Third Maximum Number](./0414-third-maximum-number.js)|Easy|
124125
442|[Find All Duplicates in an Array](./0442-find-all-duplicates-in-an-array.js)|Medium|
125126
448|[Find All Numbers Disappeared in an Array](./0448-find-all-numbers-disappeared-in-an-array.js)|Easy|
126127
451|[Sort Characters By Frequency](./0451-sort-characters-by-frequency.js)|Medium|
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* 414. Third Maximum Number
3+
* https://leetcode.com/problems/third-maximum-number/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer array nums, return the third distinct maximum number in this array.
7+
* If the third maximum does not exist, return the maximum number.
8+
*/
9+
10+
/**
11+
* @param {number[]} nums
12+
* @return {number}
13+
*/
14+
var thirdMax = function(nums) {
15+
const sortedSet = [...new Set(nums)].sort((a, b) => a - b);
16+
const thirdMax = sortedSet[sortedSet.length - 3];
17+
18+
return thirdMax !== undefined
19+
? thirdMax
20+
: sortedSet[sortedSet.length - 1];
21+
};

0 commit comments

Comments
 (0)