diff --git a/contains-duplicate/Zioq.js b/contains-duplicate/Zioq.js new file mode 100644 index 000000000..1854906cb --- /dev/null +++ b/contains-duplicate/Zioq.js @@ -0,0 +1,32 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var containsDuplicate = function(nums) { + let dup_set = new Set(nums); // Initialize Set + return dup_set.size !== nums.length + + + /* Previous Code before the review */ + // for (let num of nums) { + // dup_set.add(num) // Add value into the set (duplicated value will be ignored) + // } + + // if(dup_set.size !== nums.length) { + // return true + // } + // return false +}; + +/* + Space Complexity - O(n) - Create a set to store elements + Time Complexity - O(n) - Traverse through the array +*/ + + +/* Test code */ +console.log(containsDuplicate([1, 2, 3, 1])); // true +console.log(containsDuplicate([1, 2, 3, 4])); // false +console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // true + + diff --git a/house-robber/Zioq.js b/house-robber/Zioq.js new file mode 100644 index 000000000..214adce87 --- /dev/null +++ b/house-robber/Zioq.js @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var rob = function(nums) { + if( nums.length < 1 ) { return nums[0]} + + let prev_2 = nums[0] + let prev_1 = Math.max(nums[0], nums[1]); + + for( let i= 2; i { + if(map.has(e)) { + let current_value = map.get(e) + map.set(e, current_value +1) + } else { + map.set(e, 1) + } + }) + const store = [...map.entries()].sort((a,b) => b[1] - a[1]) + for( let i = 0 ; i < k && i < store.length ; i++ ) { + arr.push(store[i][0]) + } + return arr + +}; + + +/* Test code */ +console.log(topKFrequent([1,1,1,2,2,3],2)); // true +console.log(topKFrequent([1,2],1)); // true diff --git a/valid-palindrome/Zioq.js b/valid-palindrome/Zioq.js new file mode 100644 index 000000000..36fce16fb --- /dev/null +++ b/valid-palindrome/Zioq.js @@ -0,0 +1,33 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isPalindrome = function(s) { + + let text = s.replace(/[^0-9a-z]/gi, ''); + text = text.replace(/\s/g, '').toLowerCase(); + + const str_arr = text.split(""); + if( str_arr % 2 === 1 ) { + return false; + } + + let length = str_arr.length - 1 ; + for ( const [i, value] of str_arr.entries()) { + if( value == str_arr[length -i] ) continue; + if(value != str_arr[length - i]) { + return false + } + } + return true; +}; + +/* + Space Complexity - O(n) - Create a array to store elements + Time Complexity - O(n) - Traverse through the array +*/ + +/* Test code */ +console.log(isPalindrome("A man, a plan, a canal: Panama")); // true +console.log(isPalindrome("race a car")); // false +console.log(isPalindrome(" ")); // true