diff --git a/contains-duplicate/GUMUNYEONG.js b/contains-duplicate/GUMUNYEONG.js new file mode 100644 index 000000000..fca1f6f68 --- /dev/null +++ b/contains-duplicate/GUMUNYEONG.js @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function (nums) { + const setObj = new Set(nums); + + const diff = !(nums.length === setObj.size); + + return diff; +}; + +// TC: O(n) +// SC: O(n) diff --git a/number-of-1-bits/GUMUNYEONG.js b/number-of-1-bits/GUMUNYEONG.js new file mode 100644 index 000000000..02805653b --- /dev/null +++ b/number-of-1-bits/GUMUNYEONG.js @@ -0,0 +1,17 @@ +/** + * @param {number} n + * @return {number} + */ +var hammingWeight = function (n) { + let i = "1"; + + while (n > 2) { + n % 2 ? i += "1" : ""; + n = Math.floor(n / 2); + } + + return i.length; +}; + +// TC: O(log n) +// SC: O(1) diff --git a/top-k-frequent-elements/GUMUNYEONG.js b/top-k-frequent-elements/GUMUNYEONG.js new file mode 100644 index 000000000..0ac4a6f09 --- /dev/null +++ b/top-k-frequent-elements/GUMUNYEONG.js @@ -0,0 +1,22 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function (nums, k) { + let result = []; + let fObj = {}; + + for (let i = 0; i < nums.length; i++) { + const n = nums[i]; + fObj[n] ? fObj[n]++ : fObj[n] = 1; + } + + Object + .entries(fObj) + .sort((a, b) => b[1] - a[1]) + .slice(0, k) + .filter(v => result.push(v[0])); + + return result; +};