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
2 changes: 2 additions & 0 deletions Contains Duplicate.js → ContainsDuplicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ const containsDuplicate = function (nums) {

return newArr.some((x) => x >= 2);
};

// https://leetcode.com/problems/contains-duplicate/description/
29 changes: 29 additions & 0 deletions LongestConsecutiveSequence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Given an unsorted array of integers nums,
// return the length of the longest consecutive elements sequence.
// You must write an algorithm that runs in O(n) time.

const longestConsecutive = (nums) => {
if (!nums.length) return 0;

const x = [...new Set(nums)].sort((a, b) => a - b);
const answer = [];

let cnt = 1;

x.forEach((v, i) => {
if (v + 1 === x[i + 1]) {
cnt += 1;
} else {
answer.push(cnt);
cnt = 1;
}
});

return Math.max(...answer);
};

const nums = [1, 2, 0, 1];
const x = longestConsecutive(nums);
console.log(x);

// https://leetcode.com/problems/longest-consecutive-sequence/description/
2 changes: 2 additions & 0 deletions TwoSum.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ const nums = [3, 2, 3];
const target = 6;
const a = twoSum(nums, target);
console.log(a);

// https://leetcode.com/problems/two-sum/description/
27 changes: 27 additions & 0 deletions topKFrequent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.

const topKFrequent = (nums, k) => {
const obj = {};

for (let i = 0; i < nums.length; i++) {
const v = nums[i];
if (obj[v]) {
obj[v] += 1;
} else {
obj[v] = 1;
}
}

const sortedArray = Object.entries(obj).sort((a, b) => b[1] - a[1]);
const answer = sortedArray.slice(0, k).map(([num, _]) => Number(num));
return answer;

//[ [ '1', 3 ], [ '2', 2 ], [ '3', 3 ] ]
// 0:숫자 1:몇개가 존재하는지
// [1] 번째 인덱스 () 순서대로 정렬.
};

const nums = [1, 1, 1, 2, 2, 3, 3, 3];

const k = 2;
const a = topKFrequent(nums, k);