Skip to content

Commit

Permalink
Merge pull request #311 from GUMUNYEONG/main
Browse files Browse the repository at this point in the history
[구문영(GUMUNYEONG)] Week01 Solutions
  • Loading branch information
GUMUNYEONG authored Aug 19, 2024
2 parents e55594c + 7934db9 commit 8c4fb50
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions contains-duplicate/GUMUNYEONG.js
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions number-of-1-bits/GUMUNYEONG.js
Original file line number Diff line number Diff line change
@@ -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)
22 changes: 22 additions & 0 deletions top-k-frequent-elements/GUMUNYEONG.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 8c4fb50

Please sign in to comment.