Skip to content

Commit 2eb3c90

Browse files
authored
Merge pull request #1990 from hozzijeong/hozzijeong-patch-1
[hozzijeong] week01 solutions
2 parents 97ce36d + 8a17b65 commit 2eb3c90

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

contains-duplicate/hozzijeong.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
var containsDuplicate = function(nums) {
6+
const originLength = nums.length;
7+
const numsSet = new Set(nums);
8+
9+
return originLength !== numsSet.size;
10+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var longestConsecutive = function(nums) {
6+
const numsSet = new Set(nums);
7+
8+
9+
if(nums.length === 0) return 0
10+
11+
const sortedNums = [...numsSet].sort((a,b) => a-b);
12+
13+
const results = [];
14+
15+
let result = 1;
16+
17+
for(let i = 0; i < sortedNums.length-1; i++){
18+
const current = sortedNums[i];
19+
const next = sortedNums[i+1];
20+
21+
if((current + 1) === next){
22+
result += 1;
23+
}else{
24+
results.push(result);
25+
result = 1;
26+
}
27+
}
28+
29+
results.push(result);
30+
31+
32+
return Math.max(...results)
33+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
var topKFrequent = function(nums, k) {
7+
const numsMap = new Map();
8+
9+
for(const num of nums){
10+
const currentNums = numsMap.get(num);
11+
12+
if(!currentNums){
13+
numsMap.set(num, 1);
14+
continue;
15+
}
16+
17+
numsMap.set(num, currentNums + 1)
18+
}
19+
20+
const sortedNums = [...numsMap].sort((a,b) => b[1] - a[1]);
21+
22+
const frequencyNums = sortedNums.map(([num])=> num);
23+
24+
return frequencyNums.slice(0,k);
25+
};

two-sum/hozzijeong.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
for(let i =0; i < nums.length - 1; i++){
8+
const first = nums[i];
9+
for(let j = i + 1; j < nums.length; j++){
10+
const last = nums[j];
11+
12+
if((first + last) === target) return [i,j]
13+
}
14+
}
15+
16+
return []
17+
};

0 commit comments

Comments
 (0)