From c821214e9d7c47fe5bd12c8a94a8238da956f8b0 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Fri, 3 Jul 2026 04:51:14 +0100 Subject: [PATCH 1/6] refactored func, reduced loops --- .../calculateSumAndProduct/calculateSumAndProduct.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..d05ed35 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -18,13 +18,12 @@ */ 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; + } return { From f777073271b6b325711b0df9ccd26b845fdfde62 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Fri, 3 Jul 2026 04:54:15 +0100 Subject: [PATCH 2/6] explained the changes made --- .../calculateSumAndProduct/calculateSumAndProduct.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index d05ed35..6598c9f 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -31,3 +31,7 @@ export function calculateSumAndProduct(numbers) { product: product, }; } + +// The function has 2 loops, one for the sum and the other for the product. +// we actually don't need to loop twice, we can do both in a single loop. +// the time complexity now is 0(n) and the space complexity is O(1). \ No newline at end of file From 712e29ae3ccc544e6bfd1032e12c364cee573d6d Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Fri, 3 Jul 2026 05:18:43 +0100 Subject: [PATCH 3/6] added explaination --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..6524d17 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -12,3 +12,5 @@ export const findCommonItems = (firstArray, secondArray) => [ ...new Set(firstArray.filter((item) => secondArray.includes(item))), ]; + +// the function does not need to be optimized as it is already efficient. \ No newline at end of file From fa50d4930f0f20d5de2e87078d6336bff4c45dd7 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Fri, 3 Jul 2026 06:21:24 +0100 Subject: [PATCH 4/6] explaination --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..9ecc6c1 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -19,3 +19,6 @@ export function hasPairWithSum(numbers, target) { } return false; } + +// in this function, we need to use 2 loops to check if there is a pair of numbers that sum to the value. +// nothing can be optimized in this function, it is already efficient. \ No newline at end of file From f844cbe99d0e52a2be681094540a611937277651 Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 15 Jul 2026 09:55:15 +0100 Subject: [PATCH 5/6] solved removeDuplicates.mjs --- .../removeDuplicates/removeDuplicates.mjs | 39 +++++++------------ 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..80aa3c0 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,23 @@ /** * 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 + * @param {Array} items - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { - const uniqueItems = []; + 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 value of inputSequence) { + if (!seen.has(value)) { + seen.add(value); + uniqueItems.push(value); + } + } - return uniqueItems; + return uniqueItems; } From bba6520891d3098dd6c7b67d0d3f67d222705b4b Mon Sep 17 00:00:00 2001 From: Mohammed Abdoon Date: Wed, 15 Jul 2026 10:05:53 +0100 Subject: [PATCH 6/6] . --- Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index 80aa3c0..38af872 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,7 +1,7 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: O(n²) + * Time Complexity: O(n) * Space Complexity: O(n) * Optimal Time Complexity: O(n) * @@ -20,4 +20,4 @@ export function removeDuplicates(inputSequence) { } return uniqueItems; -} +} \ No newline at end of file