diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..1691fc4 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,22 +9,19 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: is o(n) - coz only we have one input that needs at least one loop + * Space Complexity: here the space is o(1) coz the memory will not growing whatever happen with input + * Optimal Time Complexity: here even when i drop one of the loops the optimal time complexity will be still O(n) because we dealing with input need at least one loop * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product */ export function calculateSumAndProduct(numbers) { let sum = 0; - for (const num of numbers) { - sum += num; - } - let product = 1; for (const num of numbers) { - product *= num; + sum += num; + product *= num } return { @@ -32,3 +29,6 @@ export function calculateSumAndProduct(numbers) { product: product, }; } + + +console.log(calculateSumAndProduct([2, 3, 8, 9])); diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..11fbaf6 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,17 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: now is O(n^2) - coz we doing instead loop one with the filter first array then we checking the array with includes loop to find out if the item are common in both arrays + * Space Complexity: is O(n) - coz it is grows directly proportional with the input size + * Optimal Time Complexity: to O(n), after i used a Set, now the lookup is faster instead of checking the whole second array each time * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare * @returns {Array} Array containing unique common items */ -export const findCommonItems = (firstArray, secondArray) => [ - ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +export const findCommonItems = (firstArray, secondArray) => { + let second = new Set(secondArray) + return [ ...new Set(firstArray.filter((item) => second.has(item))) ] +} +console.log(findCommonItems([1, 2, 2, 3], [4, 3, 2])) + diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..d82c307 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,27 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: is O(n^2) coz we have nested loop in the function + * Space Complexity: is O(n) coz it grows proportionally with the input size + * Optimal Time Complexity: to O(n) - and that by using Set as check point to fast lookup and catch up if it has the number instead of re-scanning everything * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find * @returns {boolean} True if pair exists, false otherwise */ export function hasPairWithSum(numbers, target) { - for (let i = 0; i < numbers.length; i++) { - for (let j = i + 1; j < numbers.length; j++) { - if (numbers[i] + numbers[j] === target) { - return true; - } + const checkPoint = new Set() + + for(let i=0; i < numbers.length; i++){ + let num = target - numbers[i] + if(checkPoint.has(num)){ + return true + }else { + checkPoint.add(numbers[i]) } } + return false; } + +console.log(hasPairWithSum([1, 2, 4, 5, 3], 9)); diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..d306749 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,18 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: is O(n^2) coz it used nested loop to remove the duplications + * Space Complexity: it is O(n) coz it is grows directly proportional with the input size + * Optimal Time Complexity: to O(n) - because even if i'm using Set, i need to go through each element in the array, which leads me to the time complexity will be growing directly proportional with the input size * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { - const uniqueItems = []; - - for ( - let currentIndex = 0; - currentIndex < inputSequence.length; - currentIndex++ - ) { - let isDuplicate = false; - for ( - let compareIndex = 0; - compareIndex < uniqueItems.length; - compareIndex++ - ) { - if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { - isDuplicate = true; - break; - } - } - if (!isDuplicate) { - uniqueItems.push(inputSequence[currentIndex]); - } - } + const uniqueItems = [...new Set(inputSequence)] return uniqueItems; } + +console.log(removeDuplicates([5, 2, 2, 3, 4, 4, 1])) +console.log(removeDuplicates(["a", "e", "b", "a", "c", "c", "a"]))