From a8156385b667e3cbdb90dd896b6eb3e99ae2b5f5 Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Tue, 3 Mar 2026 14:11:18 +0900 Subject: [PATCH 1/5] contains duplicate solution 1 --- contains-duplicate/hyejj19.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 contains-duplicate/hyejj19.ts diff --git a/contains-duplicate/hyejj19.ts b/contains-duplicate/hyejj19.ts new file mode 100644 index 0000000000..20f1f5b59f --- /dev/null +++ b/contains-duplicate/hyejj19.ts @@ -0,0 +1,12 @@ +function containsDuplicate(nums: number[]): boolean { + const numsMap = new Map(); + nums.forEach(n => { + numsMap.set(n, (numsMap.get(n) || 0) + 1); + }); + + for (const [_, count] of numsMap) { + if (count >= 2) return true; + } + + return false; +} From e1cbafe787108ceb633512f6c49d66a77e0eb50e Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Tue, 3 Mar 2026 14:13:36 +0900 Subject: [PATCH 2/5] containsDuplicate solution2 --- contains-duplicate/hyejj19.ts | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/contains-duplicate/hyejj19.ts b/contains-duplicate/hyejj19.ts index 20f1f5b59f..af954ec098 100644 --- a/contains-duplicate/hyejj19.ts +++ b/contains-duplicate/hyejj19.ts @@ -1,12 +1,3 @@ function containsDuplicate(nums: number[]): boolean { - const numsMap = new Map(); - nums.forEach(n => { - numsMap.set(n, (numsMap.get(n) || 0) + 1); - }); - - for (const [_, count] of numsMap) { - if (count >= 2) return true; - } - - return false; + return new Set(nums).size !== nums.length; } From a377f094f7159069c88ca1fc9a3da14b319709b3 Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Tue, 3 Mar 2026 14:47:09 +0900 Subject: [PATCH 3/5] twoSum solution1 --- two-sum/hyejj19.ts | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 two-sum/hyejj19.ts diff --git a/two-sum/hyejj19.ts b/two-sum/hyejj19.ts new file mode 100644 index 0000000000..17a3e74245 --- /dev/null +++ b/two-sum/hyejj19.ts @@ -0,0 +1,8 @@ +function twoSum(nums: number[], target: number): number[] { + for (let lt = 0; lt < nums.length - 1; lt++) { + for (let rt = lt + 1; rt < nums.length; rt++) { + if (nums[lt] + nums[rt] === target) return [lt, rt]; + } + } + return [0, 0]; +} From 749bbce97f1b74c6020fb63c57d9b986a8b591aa Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Tue, 3 Mar 2026 15:00:15 +0900 Subject: [PATCH 4/5] twoSum solution3 --- two-sum/hyejj19.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/two-sum/hyejj19.ts b/two-sum/hyejj19.ts index 17a3e74245..e3eae85043 100644 --- a/two-sum/hyejj19.ts +++ b/two-sum/hyejj19.ts @@ -1,8 +1,11 @@ function twoSum(nums: number[], target: number): number[] { - for (let lt = 0; lt < nums.length - 1; lt++) { - for (let rt = lt + 1; rt < nums.length; rt++) { - if (nums[lt] + nums[rt] === target) return [lt, rt]; + const table = new Map(); + nums.forEach((num, idx) => table.set(num, idx)); + + for (let i = 0; i < nums.length; i++) { + const result = target - nums[i]; + if (table.has(result) && table.get(result) !== i) { + return [i, table.get(result)]; } } - return [0, 0]; } From 2d8a742e49710f2b3e28c3e1be4a56ea24244aff Mon Sep 17 00:00:00 2001 From: hyejj19 Date: Tue, 3 Mar 2026 15:10:24 +0900 Subject: [PATCH 5/5] twoSum solution3 --- two-sum/hyejj19.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/two-sum/hyejj19.ts b/two-sum/hyejj19.ts index e3eae85043..902bf6288a 100644 --- a/two-sum/hyejj19.ts +++ b/two-sum/hyejj19.ts @@ -1,11 +1,15 @@ function twoSum(nums: number[], target: number): number[] { const table = new Map(); - nums.forEach((num, idx) => table.set(num, idx)); for (let i = 0; i < nums.length; i++) { - const result = target - nums[i]; - if (table.has(result) && table.get(result) !== i) { - return [i, table.get(result)]; + const complement = target - nums[i]; + + if (table.has(complement)) { + return [table.get(complement)!, i]; } + + table.set(nums[i], i); } + + return []; }