From 50c74a8ea90f75edef923399df85afd2087b6e21 Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:11:39 +0100 Subject: [PATCH 01/11] Refactor calculateSumAndProduct function for clarity and efficiency --- .../calculateSumAndProduct.js | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c33..6e98afb0 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -1,21 +1,42 @@ /** * 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 + * For example: [2, 3, 5] returns * { - * "sum": 10, // 2 + 3 + 5 - * "product": 30 // 2 * 3 * 5 + * sum: 10, // 2 + 3 + 5 + * product: 30 // 2 * 3 * 5 * } * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * * @param {Array} numbers - Numbers to process - * @returns {Object} Object containing running total and product + * @returns {Object} Object containing sum and product */ + +export function calculateSumAndProduct(numbers) { + let sum = 0; + let product = 1; + + for (const num of numbers) { + sum += num; + product *= num; + } + + return { + sum: sum, + product: product, + }; +} + +// Time complexity is O(n) because we visit each element once. + +// Space complexity is O(1) because we use a fixed number of variables, +// regardless of the length of the input array. + +// Optimal time complexity is O(n) because every element must be examined +// to guarantee the correct sum and product. export function calculateSumAndProduct(numbers) { let sum = 0; for (const num of numbers) { From a9341afda405f871d3baa6c4d5c6c75f16c7221f Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:11:46 +0100 Subject: [PATCH 02/11] Update documentation for findCommonItems function to clarify time and space complexities --- .../findCommonItems/findCommonItems.js | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5d..d51c0f6c 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,40 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity: O(n * m) + * Space Complexity: O(n) + * Optimal Time Complexity: O(n + m) * * @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))), ]; + +// Time complexity is O(n * m) because for each item in the first array, +// includes() may search through the entire second array. + +// Space complexity is O(n) because filter() creates a new array +// and Set stores unique common items. + +// Optimal time complexity is O(n + m) because using Sets +// allows us to check for common items more efficiently. +export function 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]; +} + +// Time complexity: O(n + m) +// Space complexity: O(n + m) +// Optimal time complexity: O(n + m) From 55a699b6b85fd9d8020675fad013035cb1b7620d Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:12:32 +0100 Subject: [PATCH 03/11] Refactor hasPairWithSum function to improve time complexity and update documentation --- .../hasPairWithSum/hasPairWithSum.js | 36 +++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f6..1503ff0a 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²) + * Space Complexity: O(1) + * Optimal Time Complexity: O(n) * * @param {Array} numbers - Array of numbers to search through * @param {number} target - Target sum to find @@ -19,3 +19,33 @@ export function hasPairWithSum(numbers, target) { } return false; } + +// Time complexity is O(n²) because we use nested loops +// to compare pairs of numbers in the array. + +// Space complexity is O(1) because we only use two variables, +// i and j, regardless of the array size. + +// Optimal time complexity is O(n) because using a Set +// allows us to find a matching pair in a single loop. + +//Improved solution-O(n) +export function hasPairWithSum(numbers, target) { + const seen = new Set(); + + for (const num of numbers) { + const needed = target - num; + + if (seen.has(needed)) { + return true; + } + + seen.add(num); + } + + return false; +} + +// Time complexity: O(n) +// Space complexity: O(n) +// Optimal time complexity: O(n) From bf5629b7158ba946cb8f01836e0d2e5c095955de Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:13:14 +0100 Subject: [PATCH 04/11] Refactor removeDuplicates function to improve time complexity and update documentation --- .../removeDuplicates/removeDuplicates.mjs | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f7711..947fa8a5 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,9 +1,10 @@ /** - * 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. * - * 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 * @returns {Array} New sequence with duplicates removed @@ -17,6 +18,7 @@ export function removeDuplicates(inputSequence) { currentIndex++ ) { let isDuplicate = false; + for ( let compareIndex = 0; compareIndex < uniqueItems.length; @@ -27,6 +29,7 @@ export function removeDuplicates(inputSequence) { break; } } + if (!isDuplicate) { uniqueItems.push(inputSequence[currentIndex]); } @@ -34,3 +37,26 @@ export function removeDuplicates(inputSequence) { return uniqueItems; } + +// Time complexity is O(n²) because we use nested loops +// to compare each item with the unique items already found. + +// Space complexity is O(n) because we create a new array +// that may store up to n unique items. + +// Optimal time complexity is O(n) because using a Set +// allows us to check for duplicates more efficiently. + +export function removeDuplicates(inputSequence) { + const uniqueItems = new Set(); + + for (const item of inputSequence) { + uniqueItems.add(item); + } + + return [...uniqueItems]; +} + +// Time complexity: O(n) on average +// Space complexity: O(n) +// Optimal time complexity: O(n) From 7a56fcbd6bfe68e5eaac30a487aee3de4c73c34f Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:14:00 +0100 Subject: [PATCH 05/11] Refactor calculate_sum_and_product function for clarity and efficiency --- .../calculate_sum_and_product.py | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 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 cfd5cfdf..88acfdce 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 @@ -1,31 +1,21 @@ from typing import Dict, List - 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} + total_sum = 0 + total_product = 1 + + for number in input_numbers: + total_sum += number + total_product *= number - sum = 0 - for current_number in input_numbers: - sum += current_number + return {"sum": total_sum, "product": total_product} - product = 1 - for current_number in input_numbers: - product *= current_number - return {"sum": sum, "product": product} +print(calculate_sum_and_product([2, 3, 5])) +# {'sum': 10, 'product': 30} +""" +Time Complexity: O(n) +Space Complexity: O(1) +Optimal Time Complexity: O(n) +""" From 749da3a7d8fc59069e7a9aa716f1e68195d5c4b7 Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:14:30 +0100 Subject: [PATCH 06/11] Refactor find_common_items function to improve efficiency and update documentation --- .../find_common_items/find_common_items.py | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index 478e2efc..ac17efed 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -2,20 +2,30 @@ ItemType = TypeVar("ItemType") - def find_common_items( - first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType] + first_sequence: Sequence[ItemType], + second_sequence: Sequence[ItemType] ) -> List[ItemType]: - """ - Find common items between two arrays. - - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - common_items: List[ItemType] = [] - for i in first_sequence: - for j in second_sequence: - if i == j and i not in common_items: - common_items.append(i) + + second_set = set(second_sequence) + common_items = [] + seen = set() + + for item in first_sequence: + if item in second_set and item not in seen: + common_items.append(item) + seen.add(item) + return common_items + + +print(find_common_items([1, 2, 3, 4], [2, 3, 5])) +# [2, 3] + +""" +Find common items between two arrays. + +Time Complexity: O(n * m * k) +Space Complexity: O(k) +Optimal Time Complexity: O(n + m) +""" \ No newline at end of file From 78e3cecd65ae615bc6f34dbb9dba5018e2b82774 Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:15:13 +0100 Subject: [PATCH 07/11] Refactor has_pair_with_sum function to improve efficiency and update documentation --- .../has_pair_with_sum/has_pair_with_sum.py | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index fe2da517..3e8fae1a 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -1,18 +1,27 @@ -from typing import List, TypeVar +def has_pair_with_sum(numbers, target_sum): -Number = TypeVar("Number", int, float) + seen = set() + for number in numbers: + complement = target_sum - number -def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: - """ - Find if there is a pair of numbers that sum to a target value. + if complement in seen: + return True + + seen.add(number) - Time Complexity: - Space Complexity: - Optimal time complexity: - """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target_sum: - return True return False + + +print(has_pair_with_sum([2, 4, 6, 8], 10)) +# True + +print(has_pair_with_sum([1, 2, 3], 10)) +# False +""" +Find if there is a pair of numbers that sum to a target value. + +Time Complexity: O(n²) +Space Complexity: O(1) +Optimal Time Complexity: O(n) +""" \ No newline at end of file From de7ed1ea1ec11ba59078fda86bd9abfe5d2a2b90 Mon Sep 17 00:00:00 2001 From: mervereis Date: Mon, 12 Oct 2026 21:15:40 +0100 Subject: [PATCH 08/11] Refactor remove_duplicates function to improve time complexity and update documentation --- .../remove_duplicates/remove_duplicates.py | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe80..5bf828b2 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -1,25 +1,24 @@ -from typing import List, Sequence, TypeVar +def remove_duplicates(values): -ItemType = TypeVar("ItemType") - - -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: - Optimal time complexity: - """ unique_items = [] + seen = set() for value in values: - is_duplicate = False - for existing in unique_items: - if value == existing: - is_duplicate = True - break - if not is_duplicate: + if value not in seen: unique_items.append(value) + seen.add(value) return unique_items + + +print(remove_duplicates([1, 2, 2, 3, 1, 4])) +# [1, 2, 3, 4] + +""" +Remove duplicate values from a sequence, +preserving the order of the first occurrence. + +Time complexity: O(n²) +Space complexity: O(n) +Optimal time complexity: O(n) +""" From decfeab597e724dfb9ebc174f05c761ea5677cae Mon Sep 17 00:00:00 2001 From: mervereis Date: Thu, 15 Oct 2026 14:01:21 +0100 Subject: [PATCH 09/11] Refactor removeDuplicates function to improve time complexity and simplify implementation --- Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index 947fa8a5..07039860 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -48,11 +48,7 @@ export function removeDuplicates(inputSequence) { // allows us to check for duplicates more efficiently. export function removeDuplicates(inputSequence) { - const uniqueItems = new Set(); - - for (const item of inputSequence) { - uniqueItems.add(item); - } + const uniqueItems = new Set(inputSequence); return [...uniqueItems]; } From 7b28f6dc4fe80aa52e679f4bb88c9494ae0ab273 Mon Sep 17 00:00:00 2001 From: mervereis Date: Thu, 15 Oct 2026 14:06:03 +0100 Subject: [PATCH 10/11] Remove print statements and example usages from multiple functions for cleaner code --- .../calculate_sum_and_product/calculate_sum_and_product.py | 2 -- Sprint-1/Python/find_common_items/find_common_items.py | 3 --- Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py | 5 ----- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 3 --- 4 files changed, 13 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 88acfdce..2d0dfd19 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 @@ -12,8 +12,6 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: return {"sum": total_sum, "product": total_product} -print(calculate_sum_and_product([2, 3, 5])) -# {'sum': 10, 'product': 30} """ Time Complexity: O(n) Space Complexity: O(1) diff --git a/Sprint-1/Python/find_common_items/find_common_items.py b/Sprint-1/Python/find_common_items/find_common_items.py index ac17efed..4fac72c9 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -19,9 +19,6 @@ def find_common_items( return common_items -print(find_common_items([1, 2, 3, 4], [2, 3, 5])) -# [2, 3] - """ Find common items between two arrays. diff --git a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py index 3e8fae1a..cdbd689d 100644 --- a/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py +++ b/Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py @@ -13,11 +13,6 @@ def has_pair_with_sum(numbers, target_sum): return False -print(has_pair_with_sum([2, 4, 6, 8], 10)) -# True - -print(has_pair_with_sum([1, 2, 3], 10)) -# False """ Find if there is a pair of numbers that sum to a target value. diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 5bf828b2..9d69f2f9 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -11,9 +11,6 @@ def remove_duplicates(values): return unique_items -print(remove_duplicates([1, 2, 2, 3, 1, 4])) -# [1, 2, 3, 4] - """ Remove duplicate values from a sequence, preserving the order of the first occurrence. From 0aae0c959802a0e1f00b708bb5b1d03ca57bf650 Mon Sep 17 00:00:00 2001 From: mervereis Date: Thu, 15 Oct 2026 14:53:22 +0100 Subject: [PATCH 11/11] Refactor findCommonItems function to improve time and space complexity, and update documentation --- .../findCommonItems/findCommonItems.js | 31 +++---------------- 1 file changed, 4 insertions(+), 27 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index d51c0f6c..a39b3155 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,40 +1,17 @@ /** * Finds common items between two arrays. * - * Time Complexity: O(n * m) - * Space Complexity: O(n) - * Optimal Time Complexity: O(n + m) + * Time Complexity: O(n + m) + * Space Complexity: O(n + m) * * @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))), -]; - -// Time complexity is O(n * m) because for each item in the first array, -// includes() may search through the entire second array. - -// Space complexity is O(n) because filter() creates a new array -// and Set stores unique common items. - -// Optimal time complexity is O(n + m) because using Sets -// allows us to check for common items more efficiently. export function findCommonItems(firstArray, secondArray) { + const firstSet = new Set(firstArray); const secondSet = new Set(secondArray); - const commonItems = new Set(); - for (const item of firstArray) { - if (secondSet.has(item)) { - commonItems.add(item); - } - } - - return [...commonItems]; + return [...firstSet.intersection(secondSet)]; } - -// Time complexity: O(n + m) -// Space complexity: O(n + m) -// Optimal time complexity: O(n + m)