From f69468c645abdf1dfb7d354a545933f31b47852e Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Mon, 2 Dec 2024 22:17:50 -0800 Subject: [PATCH 1/4] contains duplicates solution --- contains-duplicate/yoonthecoder.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 contains-duplicate/yoonthecoder.js diff --git a/contains-duplicate/yoonthecoder.js b/contains-duplicate/yoonthecoder.js new file mode 100644 index 000000000..8de1552a8 --- /dev/null +++ b/contains-duplicate/yoonthecoder.js @@ -0,0 +1,10 @@ +var containsDuplicate = function (nums) { + // Create a set from the nums array. Since Sets only allow unique values, any duplicates will be removed. + const set = new Set(nums); + // Compare the size of the set and the length of the original array.- if the size of the set is smaller than the length of the original array('nums'), it means there were duplicates. + + return set.size < nums.length; +}; + +// Time Complexity: O(n); - adding elements to the Set & compare sizes +// Space Complexity: O(n) From f863f0352f92f3ebd8e2f0b55751b883c5b04a6a Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Tue, 3 Dec 2024 23:00:21 -0800 Subject: [PATCH 2/4] valid palindrome solution --- valid-palindrome/yoonthecoder.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 valid-palindrome/yoonthecoder.js diff --git a/valid-palindrome/yoonthecoder.js b/valid-palindrome/yoonthecoder.js new file mode 100644 index 000000000..cbd8685f8 --- /dev/null +++ b/valid-palindrome/yoonthecoder.js @@ -0,0 +1,11 @@ +var isPalindrome = function (s) { + // remove any special characters and space from the string + const formattedString = s.toLowerCase().replace(/[^a-zA-Z0-9]/g, ''); + // use split() method to separate each charaters and put them in an array - reverse it - concatenate + const reversedString = formattedString.split('').reverse().join(''); + + return reversedString === formattedString; +}; + +// time complexity: O(n) +// space complexity: O(n) From a094799582ccd94f2cb227d4ef1cfbbda44c7889 Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Wed, 11 Dec 2024 15:33:50 -0800 Subject: [PATCH 3/4] top k frequent elements solution --- top-k-frequent-elements/yoonthecoder.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 top-k-frequent-elements/yoonthecoder.js diff --git a/top-k-frequent-elements/yoonthecoder.js b/top-k-frequent-elements/yoonthecoder.js new file mode 100644 index 000000000..2d297c646 --- /dev/null +++ b/top-k-frequent-elements/yoonthecoder.js @@ -0,0 +1,25 @@ +var topKFrequent = function (nums, k) { + // 1. count the frequency of each number in the array + const map = new Map(); + + // iterating through the array to count how many times each num appears. + for (const num of nums) { + // if the num already exists in the map, increment its count + if (map.has(num)) { + map.set(num, map.get(num) + 1); + } // otherwise, set it to 1 + else map.set(num, 1); + } + + // 2.create an array to store the freqeuncy numbers + const freqArr = []; + for (const [num, freq] of map) { + freqArr.push([num, freq]); + } + // sort in descending order by frequency + freqArr.sort((a, b) => b[1] - a[1]); + return freqArr.slice(0, k).map(([num]) => num); +}; + +// Time complexity: O(nlogn) +// Space complexity: O(n) From 42e2dd242710b034f158fb947e5012751d5e028a Mon Sep 17 00:00:00 2001 From: Yoon Kim Date: Thu, 12 Dec 2024 01:33:27 -0800 Subject: [PATCH 4/4] longest consecutive sequence solution --- longest-consecutive-sequence/yoonthecoder.js | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 longest-consecutive-sequence/yoonthecoder.js diff --git a/longest-consecutive-sequence/yoonthecoder.js b/longest-consecutive-sequence/yoonthecoder.js new file mode 100644 index 000000000..5c9e71256 --- /dev/null +++ b/longest-consecutive-sequence/yoonthecoder.js @@ -0,0 +1,21 @@ +var longestConsecutive = function (nums) { + // remove the duplicates from the array and sort it in an ascending order. + const setArray = [...new Set(nums)]; + const sortedArray = setArray.sort((a, b) => a - b); + // create a set to store streak lengths, even when count resets. + const countSet = new Set(); + let count = 0; + for (let i = 0; i < sortedArray.length; i++) { + if (sortedArray[i] + 1 == sortedArray[i + 1]) { + count += 1; + countSet.add(count); + } else { + count = 0; + } + } + + return nums.length === 0 ? 0 : countSet.size + 1; +}; + +// Time complexity: O(nlogn) => TODO: need to improve this to O(n) +// Space complexity: O(n)