From ddde162374590559c3158a134dce1644d37beb2d Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 21:11:32 +0000 Subject: [PATCH 01/19] Refactor: Reduce looping to 1 process that performs both operation to reduce complexity and save time --- .../calculateSumAndProduct/calculateSumAndProduct.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..2b71847 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -18,12 +18,10 @@ */ 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; } From 01895f9dbcb06b6609a44fa9a598deb7751d715d Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 21:40:44 +0000 Subject: [PATCH 02/19] explain complexity using Big O --- .../calculateSumAndProduct/calculateSumAndProduct.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 2b71847..7039fa8 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,9 +9,9 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) - Lopp through each element in an array once. + * Space Complexity: O(1) - only 2 variables used + * Optimal Time Complexity: O(n) - JS engine must read each number at least once * * @param {Array} numbers - Numbers to process * @returns {Object} Object containing running total and product From 8fafe7ba7a7dfd687a8cae9ffcb8965f767c8c99 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 21:57:17 +0000 Subject: [PATCH 03/19] Explain complexity using Big O Notation --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..4f13d60 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,9 +1,9 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n * m) - filter checks each element of firstArray and includes scans secondArray + * Space Complexity: O(n) - filter creates a new array where size grows as the input of matches grows. Same thing with set. + * Optimal Time Complexity: O(n + m) - each element must be examined at least once; using a Set allows O(1) lookups * * @param {Array} firstArray - First array to compare * @param {Array} secondArray - Second array to compare From 45371a3dcc67a37c44a9dd4669ddf5d056ca1a6d Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 22:15:21 +0000 Subject: [PATCH 04/19] convert the second array to set so that we use its methid .has() --- .../JavaScript/findCommonItems/findCommonItems.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 4f13d60..2c73c33 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -9,6 +9,13 @@ * @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) => [ +// ...new Set(firstArray.filter((item) => secondArray.includes(item))), +// ]; + + + +export const findCommonItems = (firstArray, secondArray) => { + const arrayToSet = new Set(secondArray); +}; \ No newline at end of file From 450ca3ee896abbc356c906b2275fe17a7a895ebf Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 22:16:57 +0000 Subject: [PATCH 05/19] filter through the first array element to check if the set has the same element in it --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 2c73c33..fd95ed9 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -18,4 +18,5 @@ export const findCommonItems = (firstArray, secondArray) => { const arrayToSet = new Set(secondArray); + return firstArray.filter((element) => arrayToSet.has(element)); }; \ No newline at end of file From 2ca312224ed9bcbb49c41c5e5f96db15dbdbf98d Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 22:21:46 +0000 Subject: [PATCH 06/19] update the time complexity explanation to describe the state before the code was refactored --- .../calculateSumAndProduct.js | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index 7039fa8..80c334e 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,13 +9,31 @@ * "product": 30 // 2 * 3 * 5 * } * - * Time Complexity: O(n) - Lopp through each element in an array once. + * Time Complexity: O(2n) - Since we're looping through 2 separate loops * Space Complexity: O(1) - only 2 variables used * Optimal Time Complexity: O(n) - JS engine must read each number at least once * * @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) { +// product *= num; +// } + +// return { +// sum: sum, +// product: product, +// }; +// } + export function calculateSumAndProduct(numbers) { let sum = 0; let product = 1; From a29e73efd178a9396b4b6f84fe8c26292f61a2b6 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 22:39:28 +0000 Subject: [PATCH 07/19] Explain the complexity using Big O notation --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..7a3bbc7 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,9 +1,9 @@ /** * 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²) - since it's a nested loop over the same array. + * Space Complexity: O(1) - since there's no other array, set, object that gorws proportionally to the input size, space usage doesn't grow. + * Optimal Time Complexity: O(n) - * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find From 1344256680906897a57c979c20af27e726ce5b13 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 22:52:18 +0000 Subject: [PATCH 08/19] create set to achieve time efficieny - use it to store items we've already checked while looping --- .../hasPairWithSum/hasPairWithSum.js | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 7a3bbc7..5f1dc22 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -9,13 +9,17 @@ * @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; - } - } - } - return false; -} + +// 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; +// } +// } +// } +// return false; +// } + +let checkedNumbers = new set(); + From 58344c5341971a8d9ac9f88334d93b8525ec534c Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 22:56:31 +0000 Subject: [PATCH 09/19] Loop over each element in an array and check if the set contains the second number (difference between target and first number) --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 5f1dc22..0ab9586 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -22,4 +22,14 @@ // } let checkedNumbers = new set(); +let secondNumber = target - number; + +export function hasPairWithSum(numbers, target) { + for (const number of numbers) { + if (checkedNumbers.has(secondNumber)) { + return true; + } + } + +} From 9b853cce45f2d495fad0927c7c8ecda9c52172e8 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 23:00:15 +0000 Subject: [PATCH 10/19] each loop add the check number into the set to ensure considering distinc pairs --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 0ab9586..fc22398 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -29,6 +29,7 @@ export function hasPairWithSum(numbers, target) { if (checkedNumbers.has(secondNumber)) { return true; } + checkedNumbers.add(number); } } From 67f8aabf3c206c1bcdb22afc927f64cfb704cf2a Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 23:04:28 +0000 Subject: [PATCH 11/19] refactor and move set inside the function to avoid getting wrong results --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index fc22398..4e22468 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -21,16 +21,18 @@ // return false; // } -let checkedNumbers = new set(); -let secondNumber = target - number; + export function hasPairWithSum(numbers, target) { + let checkedNumbers = new Set(); + for (const number of numbers) { - if (checkedNumbers.has(secondNumber)) { + if (checkedNumbers.has(target - number)) { return true; } checkedNumbers.add(number); } + return false; } From c778939a9175973a99bd64e6b1ceac9dd94a2754 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Wed, 25 Feb 2026 23:06:29 +0000 Subject: [PATCH 12/19] update Optimal Time Complexity explanation --- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index 4e22468..8e0b8e8 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -3,7 +3,7 @@ * * Time Complexity: O(n²) - since it's a nested loop over the same array. * Space Complexity: O(1) - since there's no other array, set, object that gorws proportionally to the input size, space usage doesn't grow. - * Optimal Time Complexity: O(n) - + * Optimal Time Complexity: O(n) - each number is checked at least once after refactoring. * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find @@ -25,7 +25,7 @@ export function hasPairWithSum(numbers, target) { let checkedNumbers = new Set(); - + for (const number of numbers) { if (checkedNumbers.has(target - number)) { return true; From 2916a36778a5820257487b7c846bc472f5dc83cd Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 00:37:47 +0000 Subject: [PATCH 13/19] Explain time and space complexities using Big O Notation --- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..4a9bbc8 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,8 +7,8 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: """ Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. - Time complexity: - Space complexity: + Time complexity: O(n2) - nested loop run once per element n times.Same for outer loop + Space complexity: O(n) - since there's another array to store unique items. its size grow proportionally as the number of unique inputs grow. Optimal time complexity: """ unique_items = [] From 6b1203edc70cf49255ea4c301bb5e040a2abe0dd Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 00:41:07 +0000 Subject: [PATCH 14/19] create a set instead of an array to store unique items. This is to avoid scanning through each element in an array which is not efficient. --- .../remove_duplicates/remove_duplicates.py | 30 +++++++++++-------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 4a9bbc8..6ca7f54 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -11,15 +11,21 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: Space complexity: O(n) - since there's another array to store unique items. its size grow proportionally as the number of unique inputs grow. Optimal time complexity: """ - unique_items = [] - - for value in values: - is_duplicate = False - for existing in unique_items: - if value == existing: - is_duplicate = True - break - if not is_duplicate: - unique_items.append(value) - - return unique_items + # unique_items = [] + + # for value in values: + # is_duplicate = False + # for existing in unique_items: + # if value == existing: + # is_duplicate = True + # break + # if not is_duplicate: + # unique_items.append(value) + + # return unique_items + + unique_items = set() + + + + \ No newline at end of file From d9501bc9d669c100b98ad045e53f36f2b456f8a5 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 00:45:11 +0000 Subject: [PATCH 15/19] add one for loop that checks if each item in the array is present in the set. if not it gets added to the set of unique numbers --- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 6ca7f54..1829075 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -25,7 +25,13 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: # return unique_items unique_items = set() + + for value in values: + if value not in unique_items: + unique_items.add(value) + return unique_items + \ No newline at end of file From 7beaf4a7ca5ae794dc891ae9717b806242a82a0d Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 00:52:18 +0000 Subject: [PATCH 16/19] update logic so that the unique items are eventually stored in an array since order matters. Also, once element is checked it gets added to the set to to avoid duplication as checking goes ahead --- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 1829075..1429af1 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -24,11 +24,13 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: # return unique_items - unique_items = set() + unique_items = [] + checked_values = set() for value in values: - if value not in unique_items: - unique_items.add(value) + if value not in checked_values: + unique_items.append(value) + checked_values.add(value) return unique_items From ac594c96cd59cd7e68af07e038931ff2a1e0f63f Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 00:56:40 +0000 Subject: [PATCH 17/19] explain optimal time complexity using Big O Notation --- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 1429af1..3771a52 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -9,7 +9,7 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: Time complexity: O(n2) - nested loop run once per element n times.Same for outer loop Space complexity: O(n) - since there's another array to store unique items. its size grow proportionally as the number of unique inputs grow. - Optimal time complexity: + Optimal time complexity: O(n) - For an array of n elements, you must look at each element at least a once to check if it's duplicate. """ # unique_items = [] From c757b24c554804eb36c58c2d21a788c9d2cbe65d Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 01:13:29 +0000 Subject: [PATCH 18/19] updading code so tests are passed and result leads to unique common/shared item --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index fd95ed9..365910d 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -18,5 +18,11 @@ export const findCommonItems = (firstArray, secondArray) => { const arrayToSet = new Set(secondArray); - return firstArray.filter((element) => arrayToSet.has(element)); + for (const element of firstArray) { + if (secondArray.includes(element)) { + arrayToSet.add(element); + } + + } + return arrayToSet; }; \ No newline at end of file From 2d34b6dbaf1454160c080acf26844776f92cff24 Mon Sep 17 00:00:00 2001 From: HassanOHOsman Date: Thu, 26 Feb 2026 01:18:44 +0000 Subject: [PATCH 19/19] update logic so that to avoid duplciation and only unique common numbers found in both arrays (1&2) are stored in uniqueCommonItems --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 365910d..2134837 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -18,11 +18,15 @@ export const findCommonItems = (firstArray, secondArray) => { const arrayToSet = new Set(secondArray); + const uniqueCommonItems = []; + const checkedItems = new Set(); + for (const element of firstArray) { - if (secondArray.includes(element)) { - arrayToSet.add(element); + if (arrayToSet.has(element) && !checkedItems.has(element)) { + checkedItems.add(element); + uniqueCommonItems.push(element); } } - return arrayToSet; + return uniqueCommonItems; }; \ No newline at end of file