From 73678d43564a3d1e93ac6153b10d49b1e8a07083 Mon Sep 17 00:00:00 2001 From: Ifeoma Date: Mon, 20 Jul 2026 08:47:17 +0200 Subject: [PATCH 1/5] Refactor calculateSumAndProduct to use a single loop --- .../calculateSumAndProduct.js | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..e7003ca 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -1,34 +1,28 @@ /** - * Calculate the sum and product of integers in a list + * Calculate the sum and product of integers in a list. * - * Note: the "sum" is every number added together - * and the "product" is every number multiplied together - * so for example: [2, 3, 5] would return - * { - * "sum": 10, // 2 + 3 + 5 - * "product": 30 // 2 * 3 * 5 - * } + * Time Complexity: O(n) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * The original solution used two separate loops. + * This version combines both calculations into a single loop, + * reducing the number of iterations while keeping constant memory usage. * * @param {Array} numbers - Numbers to process - * @returns {Object} Object containing running total and product + * @returns {Object} Object containing the sum 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, + sum, + product, }; } From 92c44a0da3f4aba5418ee004dce5dd3218c1862c Mon Sep 17 00:00:00 2001 From: Ifeoma Date: Mon, 20 Jul 2026 08:51:16 +0200 Subject: [PATCH 2/5] Refactor findCommonItems using Set for faster lookups --- .../findCommonItems/findCommonItems.js | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..f174745 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. + * Finds unique common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n + m) + * Space Complexity: O(n + k) + * Optimal Time Complexity: O(n + m) + * + * The original solution used Array.includes(), which searches + * the second array for every element in the first array. + * This version converts the second array into a Set so lookups + * are much faster. * * @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) => { + const secondSet = new Set(secondArray); + const commonItems = new Set(); + + for (const item of firstArray) { + if (secondSet.has(item)) { + commonItems.add(item); + } + } + + return [...commonItems]; +}; From c7303ce66950b4d3c3e717fedf930d8eb351fc18 Mon Sep 17 00:00:00 2001 From: Ifeoma Date: Mon, 20 Jul 2026 08:52:53 +0200 Subject: [PATCH 3/5] Refactor hasPairWithSum using Set for O(n) lookup --- .../hasPairWithSum/hasPairWithSum.js | 56 ++++++++++++++++--- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..b23bd2a 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,59 @@ /** * 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(n) + * Optimal Time Complexity: O(n) + * + * The original solution used nested loops, resulting in O(n²) time. + * This version uses a Set to store previously seen numbers, + * allowing constant-time lookups. + * + * @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) { + const seen = new Set(); + + for (const number of numbers) { + const complement = target - number; + + if (seen.has(complement)) { + return true; + } + + seen.add(number); + } + + return false; +}/** + * Find if there is a pair of numbers that sum to a given target value. + * + * Time Complexity: O(n) + * Space Complexity: O(n) + * Optimal Time Complexity: O(n) + * + * The original solution used nested loops, resulting in O(n²) time. + * This version uses a Set to store previously seen numbers, + * allowing constant-time lookups. * * @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 seen = new Set(); + + for (const number of numbers) { + const complement = target - number; + + if (seen.has(complement)) { + return true; } + + seen.add(number); } + return false; -} +} \ No newline at end of file From 2f6d1b94dbd95b55cedba30058b31a09593b1de4 Mon Sep 17 00:00:00 2001 From: Ifeoma Date: Mon, 20 Jul 2026 08:55:18 +0200 Subject: [PATCH 4/5] refactor: optimize removeDuplicates using Set for O(n) lookup --- .../removeDuplicates/removeDuplicates.mjs | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..63972d1 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,34 +1,26 @@ /** * 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) + * + * Refactor: + * The original implementation used nested loops to check for duplicates, + * resulting in O(n²) time complexity. Using a Set allows constant-time + * lookups while preserving the insertion order of unique values. * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ 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); } } From 5de24cd95571a8d764ad9d6814136b010967a82f Mon Sep 17 00:00:00 2001 From: Ifeoma Date: Mon, 20 Jul 2026 09:02:33 +0200 Subject: [PATCH 5/5] streamline calculate_sum_and_product to use a single loop for efficiency --- .../calculate_sum_and_product.py | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py index cfd5cfd..c9cb5e1 100644 --- a/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py +++ b/Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py @@ -5,27 +5,32 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: """ Calculate the sum and product of integers in a list. - Note: the sum is every number added together - and the product is every number multiplied together - so for example: [2, 3, 5] would return - { - "sum": 10, // 2 + 3 + 5 - "product": 30 // 2 * 3 * 5 - } - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - # Edge case: empty list - if not input_numbers: - return {"sum": 0, "product": 1} + Time Complexity: O(n) + Space Complexity: O(1) + Optimal Time Complexity: O(n) + + Refactor: + The original implementation iterated through the list twice: + once to calculate the sum and once to calculate the product. + This refactor combines both calculations into a single loop, + reducing the number of iterations while maintaining O(n) + time complexity. - sum = 0 - for current_number in input_numbers: - sum += current_number + Args: + input_numbers: List of integers. + Returns: + Dictionary containing the sum and product. + """ + + total_sum = 0 product = 1 - for current_number in input_numbers: - product *= current_number - return {"sum": sum, "product": product} + for number in input_numbers: + total_sum += number + product *= number + + return { + "sum": total_sum, + "product": product, + } \ No newline at end of file