From c5ac0bfe25c4ef72eb65cbe08b6ca46542e9e6c5 Mon Sep 17 00:00:00 2001 From: BangDori Date: Wed, 27 Nov 2024 19:15:08 +0900 Subject: [PATCH] =?UTF-8?q?Third=20Maximum=20Number=20=ED=92=80=EC=9D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bangdori/Third Maximum Number.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 bangdori/Third Maximum Number.js diff --git a/bangdori/Third Maximum Number.js b/bangdori/Third Maximum Number.js new file mode 100644 index 0000000..6510f17 --- /dev/null +++ b/bangdori/Third Maximum Number.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var thirdMax = function (nums) { + const sortedNums = nums.sort((a, b) => b - a); + const uniqueNums = [...new Set(sortedNums)]; + + if (uniqueNums.length <= 2) { + return uniqueNums[0]; + } + + return uniqueNums[2]; +};