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 8df437d
Showing 1 changed file with 3 additions and 7 deletions.
10 changes: 3 additions & 7 deletions missing-number/mmyeon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
*/

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

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 +29,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 8df437d

Please sign in to comment.