Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
- for문 중첩 개선
- 자료구조 map에서 set으로 변경
  • Loading branch information
mmyeon committed Jan 3, 2025
1 parent bdb42a2 commit 8a47b2f
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions missing-number/mmyeon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
*/

function missingNumber(nums: number[]): number {
const map = new Map();
nums.forEach((num) => map.set(num, 1));
const set = new Set();
nums.forEach((num) => set.add(num));

for (let i = 0; i < nums.length; i++) {
if (!map.has(i)) return i;
if (!set.has(i)) return i;
}

return nums.length;
Expand All @@ -30,10 +30,7 @@ function missingNumber(nums: number[]): number {

for (let i = 0; i <= nums.length; i++) {
sum += i;
}

for (let i = 0; i < nums.length; i++) {
sum -= nums[i];
if (i < nums.length) sum -= nums[i];
}

return sum;
Expand Down

0 comments on commit 8a47b2f

Please sign in to comment.