From d6e47fe6922b5c39ca6e1fc5f3c0fcf09d8dee90 Mon Sep 17 00:00:00 2001 From: Heesun Lee Date: Fri, 6 Mar 2026 21:18:12 -0800 Subject: [PATCH 1/3] contains duplicate solution --- contains-duplicate/heesun-task.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 contains-duplicate/heesun-task.js diff --git a/contains-duplicate/heesun-task.js b/contains-duplicate/heesun-task.js new file mode 100644 index 000000000..9dbce67dd --- /dev/null +++ b/contains-duplicate/heesun-task.js @@ -0,0 +1,26 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ + /* + Goal: return true if duplicated #, false if no duplicated number + + Plan: + - create a Set + - loop num in nums + - num exists in the Set -> return true + - not exsits -> add the num in the Set, continue to the next loop + - return false + + space complexity: O(n) + time complexity: O(n) + */ +var containsDuplicate = function(nums) { + const seen = new Set(); + + for(const num of nums) { + if (seen.has(num)) return true; + seen.add(num); + } + return false; +}; \ No newline at end of file From 1e540cd3e27b4b0525ac280aff915cb554a152e6 Mon Sep 17 00:00:00 2001 From: Heesun Lee Date: Fri, 6 Mar 2026 21:20:32 -0800 Subject: [PATCH 2/3] Add trailing newline at end of file --- contains-duplicate/heesun-task.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contains-duplicate/heesun-task.js b/contains-duplicate/heesun-task.js index 9dbce67dd..ac5eb4b39 100644 --- a/contains-duplicate/heesun-task.js +++ b/contains-duplicate/heesun-task.js @@ -23,4 +23,4 @@ var containsDuplicate = function(nums) { seen.add(num); } return false; -}; \ No newline at end of file +}; From 44a3f84d349eb2a51c5a3e042e9117b41e261c71 Mon Sep 17 00:00:00 2001 From: Heesun Lee Date: Fri, 6 Mar 2026 21:25:42 -0800 Subject: [PATCH 3/3] #1 two sum solution --- two-sum/heesun-task.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 two-sum/heesun-task.js diff --git a/two-sum/heesun-task.js b/two-sum/heesun-task.js new file mode 100644 index 000000000..40a36cca1 --- /dev/null +++ b/two-sum/heesun-task.js @@ -0,0 +1,33 @@ +/* + Goal: return indices of the two numbers such that they add up to target + + Plan: + - create a hash map (dict) to store number -> index + - loop through nums with index i + - get currentNum = nums[i] + - compute pairNum = target - currentNum + - if pairNum exists in dict + -> return [dict[pairNum], i] + - otherwise store currentNum in dict with its index + - if no pair is found, return [-1, -1] + + space complexity: O(n) + time complexity: O(n) +*/ + +var twoSum = function(nums, target) { + const dict = {}; + + for (let i = 0; i < nums.length; i++) { + const currentNum = nums[i]; + const pairNum = target - currentNum; + + if (typeof dict[pairNum] === 'number') { + return [dict[pairNum], i]; + } else { + dict[currentNum] = i; + } + } + + return [-1, -1]; +};