From 620c969994aa53128875331f68e1e42394135be4 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Sun, 11 Oct 2026 14:50:26 +0100 Subject: [PATCH 1/5] function refactored and explained complexity --- .../calculateSumAndProduct.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c33..70d2e1b3 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,24 +9,24 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) - one loop through the list (the original used two loops, 2n steps, which is also O(n)). + * Space Complexity: O(1) - only two variables (sum, product), regardless of input size + * Optimal Time Complexity: O(n) - every number must be read at least once, so we can't do better than linear time + * + * Refactor: the complexity can't be reduced, but merging the two loops into + * one makes the code cleaner and does half the steps. * * @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 b53889221150d6d1bca42b28028db40e961ec78b Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Sun, 11 Oct 2026 17:34:06 +0100 Subject: [PATCH 2/5] Refactor findCommonItems to O(n + m) using a Set --- .../findCommonItems/findCommonItems.js | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5d..7d26f784 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,35 @@ /** * Finds common items between two arrays. - * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: - * + + * Inefficiency: the original used secondArray.includes() inside firstArray.filter(), which is a loop inside a loop. + + * Originally: + * Time Complexity: O(n × m) - filter runs n times and each includes call can scan up to m items (O(n²) if the arrays are the same size). + + * Space Complexity: O(n) - the filtered array and the Set hold up to n items. + + * Optimal Time Complexity: O(n + m) - every item in both arrays must be read at least once + + * After refactor (the complexity was reduced from quadratic to linear time by + using a Set for O(1) average lookups instead of includes): + + * Time Complexity: O(n + m) - building secondSet is O(m); the loop over firstArray is O(n) with O(1) average has() checks. + +* Space Complexity: O(n + m) - secondSet holds up to m items; commonElements and the final Set hold up to n items. +* Optimal Time Complexity: O(n + m) - every item in both arrays must be read at least once, so we can't do better than linear + * * @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); +let commonElements = []; + +for(let ele of firstArray){ + if(secondSet.has(ele)){ + commonElements.push(ele); + } +} +return [...new Set(commonElements)]; +}; From 749e9e082a97e241eb709aefbe48b09e833dd380 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Sun, 11 Oct 2026 18:44:53 +0100 Subject: [PATCH 3/5] =?UTF-8?q?Refactor=20hasPairWithSum=20from=20O(n?= =?UTF-8?q?=C2=B2)=20to=20O(n)=20using=20a=20Set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hasPairWithSum/hasPairWithSum.js | 40 ++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f6..df09ee81 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,43 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Inefficiency: the original used a loop inside a loop to check every + * possible pair of numbers. + * + * Originally: + * Time Complexity: O(n²) - for each number, the inner loop scans the + * remaining numbers, so about n * (n - 1) / 2 pair checks. + * + * Space Complexity: O(1) - only the loop counters i and j are used. + * + * Optimal Time Complexity: O(n) - every number must be read at least once, + * and a Set gives O(1) average lookups, so one pass is enough + * + * After Refactor (reduced from quadratic to linear time by remembering the + * numbers already seen in a Set instead of re-scanning the list): + * + * Time Complexity: O(n) - one loop, with O(1) average has() and add(). + * + * Space Complexity: O(n) - visitedNumbers can hold up to n numbers. + * + * Optimal Time Complexity: O(n) - every number must be read at least once, + * so we can't do better than linear. * * @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 visitedNumbers = new Set(); + + for (let num of numbers) { + const partnerNum = target - num; + + if (visitedNumbers.has(partnerNum)) { + return true; } + visitedNumbers.add(num); } + return false; -} +} \ No newline at end of file From c165a716dd682e94a14ecc67108ea7bc7ae34e05 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Sun, 11 Oct 2026 19:13:17 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Refactor=20removeDuplicates=20from=20O(n?= =?UTF-8?q?=C2=B2)=20to=20O(n)=20using=20a=20Set?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../removeDuplicates/removeDuplicates.mjs | 46 ++++++++----------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f7711..8ed92312 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,28 @@ /** * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: +/** +* Originally (nested loops): +* Time Complexity: O(n²) - the outer loop runs n times and the inner loop +* scans uniqueItems, which can grow up to n items (about n(n - 1) / 2 +* checks in the worst case, when all items are unique) +* Space Complexity: O(n) - uniqueItems can hold up to n items +* Optimal Time Complexity: O(n) - every item must be read at least once, and +* a Set gives O(1) average lookups + +* After refactor ([...new Set(inputSequence)]): +* Time Complexity: O(n) - building the Set visits each item once with O(1) +* average inserts; spreading it into an array is also O(n) +* Space Complexity: O(n) - the Set and the result array each hold up to n items +* Optimal Time Complexity: O(n) - every item must be read at least once, so we +* can't do better than linear +* / + * * * @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]); - } - } - - return uniqueItems; + + return [... new Set(inputSequence)]; } From 82c2feccb210b4389194e65139006d0ceff23591 Mon Sep 17 00:00:00 2001 From: RomanSanaye Date: Wed, 14 Oct 2026 23:24:03 +0100 Subject: [PATCH 5/5] refactor: clean up formatting and improve JSDoc comments --- .../calculateSumAndProduct.js | 13 +++-- .../findCommonItems/findCommonItems.js | 50 +++++++++++-------- .../hasPairWithSum/hasPairWithSum.js | 18 +++---- .../removeDuplicates/removeDuplicates.mjs | 37 +++++++------- 4 files changed, 65 insertions(+), 53 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 70d2e1b3..e874565c 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,9 +9,13 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: O(n) - one loop through the list (the original used two loops, 2n steps, which is also O(n)). - * Space Complexity: O(1) - only two variables (sum, product), regardless of input size - * Optimal Time Complexity: O(n) - every number must be read at least once, so we can't do better than linear time + * Time Complexity: O(n) - one loop through the list (the original used two + * loops, 2n steps, which is also O(n)). + * + * Space Complexity: O(1) - only two variables (sum, product), regardless of + * input size + * + * Optimal Time Complexity: O(n) - every number must be read at least once, so * we can't do better than linear time * * Refactor: the complexity can't be reduced, but merging the two loops into * one makes the code cleaner and does half the steps. @@ -22,11 +26,12 @@ export function calculateSumAndProduct(numbers) { let sum = 0; let product = 1; + for (const num of numbers) { sum += num; product *= num; } - + return { sum: sum, product: product, diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 7d26f784..6c6dc83d 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,35 +1,41 @@ /** - * Finds common items between two arrays. +* Finds common items between two arrays. - * Inefficiency: the original used secondArray.includes() inside firstArray.filter(), which is a loop inside a loop. +* Inefficiency: the original used secondArray.includes() inside firstArray +filter(), which is a loop inside a loop. - * Originally: - * Time Complexity: O(n × m) - filter runs n times and each includes call can scan up to m items (O(n²) if the arrays are the same size). +* Originally: +* Time Complexity: O(n × m) - filter runs n times and each includes call can +* scan up to m items (O(n²) if the arrays are the same size). - * Space Complexity: O(n) - the filtered array and the Set hold up to n items. +* Space Complexity: O(n) - the filtered array and the Set hold up to n items. - * Optimal Time Complexity: O(n + m) - every item in both arrays must be read at least once +* Optimal Time Complexity: O(n + m) - every item in both arrays must be read at +least once. - * After refactor (the complexity was reduced from quadratic to linear time by - using a Set for O(1) average lookups instead of includes): +* After refactor (the complexity was reduced from quadratic to linear time by +using a Set for O(1) average lookups instead of includes): - * Time Complexity: O(n + m) - building secondSet is O(m); the loop over firstArray is O(n) with O(1) average has() checks. +* Time Complexity: O(n + m) - building secondSet is O(m); the loop over +firstArray is O(n) with O(1) average has() checks. + +* Space Complexity: O(n + m) - secondSet holds up to m items; commonElements +and the final Set hold up to n items. -* Space Complexity: O(n + m) - secondSet holds up to m items; commonElements and the final Set hold up to n items. * Optimal Time Complexity: O(n + m) - every item in both arrays must be read at least once, so we can't do better than linear - * - * @param {Array} firstArray - First array to compare - * @param {Array} secondArray - Second array to compare - * @returns {Array} Array containing unique common items - */ +* +* @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) => { -const secondSet = new Set(secondArray); -let commonElements = []; + const secondSet = new Set(secondArray); + let commonElements = []; -for(let ele of firstArray){ - if(secondSet.has(ele)){ - commonElements.push(ele); + for (let ele of firstArray) { + if (secondSet.has(ele)) { + commonElements.push(ele); + } } -} -return [...new Set(commonElements)]; + return [...new Set(commonElements)]; }; diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index df09ee81..a24e4f1e 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -6,22 +6,22 @@ * * Originally: * Time Complexity: O(n²) - for each number, the inner loop scans the - * remaining numbers, so about n * (n - 1) / 2 pair checks. - * + * remaining numbers, so about n * (n - 1) / 2 pair checks. + * * Space Complexity: O(1) - only the loop counters i and j are used. - * + * * Optimal Time Complexity: O(n) - every number must be read at least once, - * and a Set gives O(1) average lookups, so one pass is enough + * and a Set gives O(1) average lookups, so one pass is enough * * After Refactor (reduced from quadratic to linear time by remembering the * numbers already seen in a Set instead of re-scanning the list): - * + * * Time Complexity: O(n) - one loop, with O(1) average has() and add(). - * + * * Space Complexity: O(n) - visitedNumbers can hold up to n numbers. - * + * * Optimal Time Complexity: O(n) - every number must be read at least once, - * so we can't do better than linear. + * so we can't do better than linear. * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find @@ -40,4 +40,4 @@ export function hasPairWithSum(numbers, target) { } return false; -} \ No newline at end of file +} diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index 8ed92312..68feb30b 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,28 +1,29 @@ /** - * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. - * -/** +* Remove duplicate values from a sequence, preserving the order of the first +* occurrence of each value. +* * Originally (nested loops): * Time Complexity: O(n²) - the outer loop runs n times and the inner loop -* scans uniqueItems, which can grow up to n items (about n(n - 1) / 2 -* checks in the worst case, when all items are unique) -* Space Complexity: O(n) - uniqueItems can hold up to n items +* scans uniqueItems, which can grow up to n items (about n(n - 1) / 2 +* checks in the worst case, when all items are unique). +* +* Space Complexity: O(n) - uniqueItems can hold up to n items. +* * Optimal Time Complexity: O(n) - every item must be read at least once, and -* a Set gives O(1) average lookups +* a Set gives O(1) average lookups. * After refactor ([...new Set(inputSequence)]): * Time Complexity: O(n) - building the Set visits each item once with O(1) -* average inserts; spreading it into an array is also O(n) -* Space Complexity: O(n) - the Set and the result array each hold up to n items +* average inserts; spreading it into an array is also O(n). +* +* Space Complexity: O(n) - the Set and the result array each hold up to n items. +* * Optimal Time Complexity: O(n) - every item must be read at least once, so we -* can't do better than linear -* / - * - * - * @param {Array} inputSequence - Sequence to remove duplicates from - * @returns {Array} New sequence with duplicates removed - */ +* can't do better than linear. +* +* @param {Array} inputSequence - Sequence to remove duplicates from +* @returns {Array} New sequence with duplicates removed +*/ export function removeDuplicates(inputSequence) { - - return [... new Set(inputSequence)]; + return [...new Set(inputSequence)]; }