From 4a809cffd67a5c62f0582126b62d301c23f17ed7 Mon Sep 17 00:00:00 2001 From: Iheb Hamdi <67348203+hamdiheb@users.noreply.github.com> Date: Wed, 15 Jul 2026 10:43:10 +0200 Subject: [PATCH 1/4] Refactored calculateSumAndProduct to make the app more effecient and less expensive --- .../calculateSumAndProduct.js | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..451a13b 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -5,30 +5,36 @@ * 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 + * "sum": 10, + * "product": 30 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) + * The function loops through the array once, processing each element exactly once. + * + * Space Complexity: O(1) + * Only two variables (sum and product) are used, regardless of the input size. + * + * Optimal Time Complexity: O(n) + * This is the optimal time complexity because every element must be read at least + * once to calculate both the sum and the product. * * @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 sum = 0 + let product = 1 - let product = 1; + // Refactored to use a single loop instead of two. + // This reduces the number of iterations but keeps the time complexity at O(n). for (const num of numbers) { - product *= num; + sum += num + product *= num } return { - sum: sum, - product: product, - }; + sum, + product, + } } From fae7582662f261450ea46424e5cc07db01784a2e Mon Sep 17 00:00:00 2001 From: Iheb Hamdi <67348203+hamdiheb@users.noreply.github.com> Date: Wed, 15 Jul 2026 10:48:53 +0200 Subject: [PATCH 2/4] FindcommonItems can not be refactored because the function already following the best code complexity --- .../JavaScript/findCommonItems/findCommonItems.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..b67766b 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,9 +1,16 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n × m) + * The function loops through the first array and uses `includes()` to search + * the second array for each element. + * + * Space Complexity: O(n) + * A new array is created to store the matching items. + * + * Optimal Time Complexity: O(n × m) + * The complexity cannot be reduced without using an additional lookup + * data structure such as a Set or a hash map. * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare @@ -11,4 +18,4 @@ */ export const findCommonItems = (firstArray, secondArray) => [ ...new Set(firstArray.filter((item) => secondArray.includes(item))), -]; +] From 056d7e2d238d22523b76c11fc44d4627e7609c3a Mon Sep 17 00:00:00 2001 From: Iheb Hamdi <67348203+hamdiheb@users.noreply.github.com> Date: Wed, 15 Jul 2026 10:54:00 +0200 Subject: [PATCH 3/4] Refactoring haspairwithsum through set class --- .../hasPairWithSum/hasPairWithSum.js | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..59f9c6f 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,29 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Original Time Complexity: O(n²) + * The original code uses nested loops to compare every possible pair of numbers. + * + * Space Complexity: O(n) + * A Set is used to store numbers that have already been seen. + * + * Optimal Time Complexity: O(n) + * The array is traversed once, and Set lookups are O(1) on average. * * @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 num of numbers) { + if (seen.has(target - num)) { + return true } + + seen.add(num) } - return false; + + return false } From 36769e7fb57199a5f84ed0e27ce0f657aedcbd41 Mon Sep 17 00:00:00 2001 From: Iheb Hamdi <67348203+hamdiheb@users.noreply.github.com> Date: Wed, 15 Jul 2026 10:56:30 +0200 Subject: [PATCH 4/4] Refactoring nested loops to one set --- .../removeDuplicates/removeDuplicates.mjs | 43 ++++++------------- 1 file changed, 13 insertions(+), 30 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..bb55809 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,19 @@ /** - * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. + * Remove duplicate items from a sequence. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Original Time Complexity: O(n²) + * The original code uses a nested loop to compare every item with the + * previously collected unique items. * - * @param {Array} inputSequence - Sequence to remove duplicates from - * @returns {Array} New sequence with duplicates removed + * Space Complexity: O(n) + * A new collection is created to store unique items. + * + * Optimal Time Complexity: O(n) + * A Set allows constant-time lookups, so each item only needs to be checked once. + * + * @param {Array} inputSequence - Sequence containing possible duplicates + * @returns {Array} Sequence containing only unique items */ 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]); - } - } - - return uniqueItems; + return [...new Set(inputSequence)] }