diff --git a/contains-duplicate/hozzijeong.js b/contains-duplicate/hozzijeong.js new file mode 100644 index 0000000000..47da62bf1d --- /dev/null +++ b/contains-duplicate/hozzijeong.js @@ -0,0 +1,10 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + const originLength = nums.length; + const numsSet = new Set(nums); + + return originLength !== numsSet.size; +}; diff --git a/longest-consecutive-sequence/hozzijeong.js b/longest-consecutive-sequence/hozzijeong.js new file mode 100644 index 0000000000..9f7c468a62 --- /dev/null +++ b/longest-consecutive-sequence/hozzijeong.js @@ -0,0 +1,33 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var longestConsecutive = function(nums) { + const numsSet = new Set(nums); + + + if(nums.length === 0) return 0 + + const sortedNums = [...numsSet].sort((a,b) => a-b); + + const results = []; + + let result = 1; + + for(let i = 0; i < sortedNums.length-1; i++){ + const current = sortedNums[i]; + const next = sortedNums[i+1]; + + if((current + 1) === next){ + result += 1; + }else{ + results.push(result); + result = 1; + } + } + + results.push(result); + + + return Math.max(...results) +}; diff --git a/top-k-frequent-elements/hozzijeong.js b/top-k-frequent-elements/hozzijeong.js new file mode 100644 index 0000000000..cd1e51866d --- /dev/null +++ b/top-k-frequent-elements/hozzijeong.js @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var topKFrequent = function(nums, k) { + const numsMap = new Map(); + + for(const num of nums){ + const currentNums = numsMap.get(num); + + if(!currentNums){ + numsMap.set(num, 1); + continue; + } + + numsMap.set(num, currentNums + 1) + } + + const sortedNums = [...numsMap].sort((a,b) => b[1] - a[1]); + + const frequencyNums = sortedNums.map(([num])=> num); + + return frequencyNums.slice(0,k); +}; diff --git a/two-sum/hozzijeong.js b/two-sum/hozzijeong.js new file mode 100644 index 0000000000..5b9178e07e --- /dev/null +++ b/two-sum/hozzijeong.js @@ -0,0 +1,17 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + for(let i =0; i < nums.length - 1; i++){ + const first = nums[i]; + for(let j = i + 1; j < nums.length; j++){ + const last = nums[j]; + + if((first + last) === target) return [i,j] + } + } + + return [] +};