Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions contains-duplicate/Kyojin-Hwang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
const map = new Map();

for (let num of nums) {
map.set(num, (map.get(num) || 0) + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

js에서 Map 활용을 잘하시고 계시네요 👍🏻

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니댱!!

}

for (let [_, count] of map) {
if (count > 1) return true;
}

return false;
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var containsDuplicate = function (nums) {
  const seen = new Set();
  for (let num of nums) {
    if (seen.has(num)) return true;
    seen.add(num);
  }
  return false;
};

Set 자료구조를 사용한다면 중복을 제거하고 메모리, 속도 측면에서 좋다고 생각합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋은 의견 감사합니다!

// set 방식으로도 대체가능
// var containsDuplicate = function(nums) {
// return new Set(nums).size !== nums.length;
// };
23 changes: 23 additions & 0 deletions top-k-frequent-elements/Kyojin-Hwang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var topKFrequent = function (nums, k) {
const map = new Map();
const result = [];

for (let num of nums) {
map.set(num, (map.get(num) || 0) + 1);
}

const sorted = [...map.entries()]
.sort((a, b) => b[1] - a[1]) // 빈도 기준으로 정렬
.map((entry) => entry[0]); // 숫자만 추출

for (let i = 0; i < k; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return sorted.slice(0, k);

slice를 활용하면 바로 반환도 가능합니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 그런 방법이 있었네용!!

result.push(sorted[i]);
}

return result;
};
18 changes: 18 additions & 0 deletions two-sum/Kyojin-Hwang.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function (nums, target) {
const map = new Map();

for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];

if (map.has(complement)) {
return [map.get(complement), i];
}

map.set(nums[i], i);
}
};