From 7fe96f43f5b1dff21c6ed46cc32bb7d1ead2fb46 Mon Sep 17 00:00:00 2001 From: Suha Date: Tue, 10 Dec 2024 23:28:13 -0400 Subject: [PATCH 1/6] contains duplicate solution --- contains-duplicate/suhacs.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 contains-duplicate/suhacs.js diff --git a/contains-duplicate/suhacs.js b/contains-duplicate/suhacs.js new file mode 100644 index 000000000..d85e0837d --- /dev/null +++ b/contains-duplicate/suhacs.js @@ -0,0 +1,9 @@ +function containsDuplicate(nums) { + const setLength = [...new Set(nums)].length; + const numLength = nums.length; + + if (numLength === setLength) return false; + else return true; +} + +console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); From 5c3e52547d6da210130561437724b49ec2b9306f Mon Sep 17 00:00:00 2001 From: Suha Date: Tue, 10 Dec 2024 23:53:22 -0400 Subject: [PATCH 2/6] contains duplicate solution --- contains-duplicate/suhacs.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contains-duplicate/suhacs.js b/contains-duplicate/suhacs.js index d85e0837d..a433866d5 100644 --- a/contains-duplicate/suhacs.js +++ b/contains-duplicate/suhacs.js @@ -7,3 +7,5 @@ function containsDuplicate(nums) { } console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); + +// From 892a2792644b284fcb44db4392a0bf8c5e3f0c1b Mon Sep 17 00:00:00 2001 From: Suha Date: Tue, 10 Dec 2024 23:57:33 -0400 Subject: [PATCH 3/6] contains duplicate solution --- contains-duplicate/suhacs.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/contains-duplicate/suhacs.js b/contains-duplicate/suhacs.js index a433866d5..75481ee49 100644 --- a/contains-duplicate/suhacs.js +++ b/contains-duplicate/suhacs.js @@ -1,3 +1,16 @@ +/** + * @description + * time complexity: O(n) + * space complexity: O(n) + * approach/strategy: + * 1. brute force + hash table + */ + +/** + * @param {number[]} nums + * @return {boolean} + */ + function containsDuplicate(nums) { const setLength = [...new Set(nums)].length; const numLength = nums.length; From d1fbcfb2dc0b6651b1f19b6ea173a5a0da456bae Mon Sep 17 00:00:00 2001 From: suhacs Date: Wed, 11 Dec 2024 16:25:33 -0500 Subject: [PATCH 4/6] contains duplicate --- contains-duplicate/suhacs.js | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/contains-duplicate/suhacs.js b/contains-duplicate/suhacs.js index 75481ee49..f018c8820 100644 --- a/contains-duplicate/suhacs.js +++ b/contains-duplicate/suhacs.js @@ -1,24 +1,7 @@ -/** - * @description - * time complexity: O(n) - * space complexity: O(n) - * approach/strategy: - * 1. brute force + hash table - */ - -/** - * @param {number[]} nums - * @return {boolean} - */ - function containsDuplicate(nums) { const setLength = [...new Set(nums)].length; const numLength = nums.length; - - if (numLength === setLength) return false; - else return true; + return numLength === setLength ? false : true; } - console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); - // From 947ca5b44a9671de8c1b2e23fa1f86075c6586a0 Mon Sep 17 00:00:00 2001 From: suhacs Date: Thu, 12 Dec 2024 14:27:35 -0500 Subject: [PATCH 5/6] valid-palindrome --- valid-palindrome/suhacs.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 valid-palindrome/suhacs.js diff --git a/valid-palindrome/suhacs.js b/valid-palindrome/suhacs.js new file mode 100644 index 000000000..cc4508004 --- /dev/null +++ b/valid-palindrome/suhacs.js @@ -0,0 +1,12 @@ +function isValidPalindrome(text) { + let refinedTxt = text + .replaceAll(/[^a-zA-Z0-9]/g, "") + .replaceAll(" ", "") + .toLowerCase(); + console.log(refinedTxt === [...refinedTxt].reverse().join("") ? true : false); + return refinedTxt === [...refinedTxt].reverse().join("") ? true : false; +} + +isValidPalindrome("A man, a plan, a canal: Panama"); //true +isValidPalindrome("race a car"); //false +isValidPalindrome(" "); //true From 958eaf193dfb21f52663345a348e29e002921b5b Mon Sep 17 00:00:00 2001 From: suhacs Date: Thu, 12 Dec 2024 16:57:41 -0500 Subject: [PATCH 6/6] top-k-frequent-elements : still working --- top-k-frequent-elements/suhacs.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 top-k-frequent-elements/suhacs.js diff --git a/top-k-frequent-elements/suhacs.js b/top-k-frequent-elements/suhacs.js new file mode 100644 index 000000000..13bf4d287 --- /dev/null +++ b/top-k-frequent-elements/suhacs.js @@ -0,0 +1,14 @@ +// Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order. + +function top_k_frequent_element(numArr, k) { + let element_qty = []; + const numSet = [...new Set(numArr)]; + for (num of numSet) { + const count = numArr.filter((x) => x === num).length; + element_qty.push({ [num]: count }); + } + Object.keys(element_qty).forEach((key) => element_qty[key]); +} + +const Arr = [1, 2, 3, 4, 5, 5, 5, 5, 3, 3, 32, 2, 2, 1]; +top_k_frequent_element(Arr);