From a1258e0204a6fe6e801ca53c4f84dec08349b12a Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Sun, 5 Jul 2026 23:03:56 +0100 Subject: [PATCH 1/5] Refactor calculateSumAndProduct --- .../calculateSumAndProduct.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..bf252e6 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,24 +9,25 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: - * + * Time Complexity: O(n) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) + * + * We must visit every number at least once, + * so O(n) is already optimal. + * @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) { + sum += num; product *= num; } - + return { sum: sum, product: product, From c7b1e5502de5f570dd8243a9591a252e6677c125 Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Sun, 5 Jul 2026 23:12:32 +0100 Subject: [PATCH 2/5] explnation added --- .../calculateSumAndProduct/calculateSumAndProduct.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index bf252e6..8909267 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -27,9 +27,16 @@ export function calculateSumAndProduct(numbers) { sum += num; product *= num; } - + return { sum: sum, product: product, }; } +/* +Time Complexity is O(n) because every number in the array must be +processed once. Space Complexity is O(1) because only two variables + (sum and product) are stored regardless of the array size. O(n) is + also the optimal complexity because every element must be examined + at least once to calculate the correct sum and product. + */ \ No newline at end of file From 0c96de0b6e9c891385b27e89e85c48b01adc6522 Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Sun, 5 Jul 2026 23:25:46 +0100 Subject: [PATCH 3/5] Refactor findCommonItems using Set lookup --- .../findCommonItems/findCommonItems.js | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..89ece36 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,28 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) + * Space Complexity: O(n) + * Optimal Time Complexity: O(n + m) + * + * Original complexity: + * The original solution used filter() and includes(). + * filter() loops through every item in firstArray. + * For each item, includes() may need to search the entire + * secondArray. This results in O(n × m) time, which is + * O(n²) when both arrays are similar in size. * - * @param {Array} firstArray - First array to compare - * @param {Array} secondArray - Second array to compare - * @returns {Array} Array containing unique common items + * Refactor: + * Convert secondArray into a Set. Set.has() provides + * constant-time lookups, so we only loop through each + * array once. This reduces the time complexity to O(n + m). + * + * Space Complexity: + * We store secondArray in a Set, which requires extra + * memory proportional to the size of secondArray. */ -export const findCommonItems = (firstArray, secondArray) => [ - ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +export const findCommonItems = (firstArray, secondArray) => { + const secondSet = new Set(secondArray); + + return [...new Set(firstArray.filter((item) => secondSet.has(item)))]; +}; \ No newline at end of file From c3a6f11c9d0cbb9a62053a10f2549ac54fa95d37 Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Sun, 5 Jul 2026 23:30:31 +0100 Subject: [PATCH 4/5] Refactor hasPairWithSum using Set --- .../hasPairWithSum/hasPairWithSum.js | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..8b01b85 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,40 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n²) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * - * @param {Array} numbers - Array of numbers to search through - * @param {number} target - Target sum to find - * @returns {boolean} True if pair exists, false otherwise + * Original complexity: + * The original solution uses nested loops. For each number, + * it checks every remaining number in the array looking for + * a pair whose sum equals the target. Since each number can + * be compared with many other numbers, the time complexity + * is O(n²). + * + * Refactor: + * Use a Set to store numbers that have already been seen. + * For each number, calculate the value needed to reach the + * target and check whether it exists in the Set. + * Set lookups are O(1), so we only need one pass through + * the array. + * + * Refactored Complexity: + * Time Complexity: O(n) + * Space Complexity: O(n) */ 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 seen = new Set(); + + for (const num of numbers) { + const needed = target - num; + + if (seen.has(needed)) { + return true; } + + seen.add(num); } + return false; -} +} \ No newline at end of file From 2a57b1198978258a8974caa57ed209650933b99c Mon Sep 17 00:00:00 2001 From: Zobeir-Rigi Date: Sun, 5 Jul 2026 23:32:40 +0100 Subject: [PATCH 5/5] Refactor removeDuplicates using Set --- .../removeDuplicates/removeDuplicates.mjs | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..905dd26 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,35 @@ /** * 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: O(n²) + * Space Complexity: O(n) + * Optimal Time Complexity: O(n) * - * @param {Array} inputSequence - Sequence to remove duplicates from - * @returns {Array} New sequence with duplicates removed + * Original complexity: + * The original solution uses nested loops. For each item in + * the input sequence, it searches through the uniqueItems + * array to check whether the value already exists. This + * results in O(n²) time complexity in the worst case. + * + * Refactor: + * Use a Set to keep track of values that have already been + * seen. Set.has() and Set.add() are O(1) operations, allowing + * us to process the sequence in a single loop. + * + * Refactored Complexity: + * Time Complexity: O(n) + * Space Complexity: O(n) */ export function removeDuplicates(inputSequence) { + const seen = new Set(); 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]); + for (const item of inputSequence) { + if (!seen.has(item)) { + seen.add(item); + uniqueItems.push(item); } } return uniqueItems; -} +} \ No newline at end of file