From f721493723ae84b985a19aeae49dc53cebce421d Mon Sep 17 00:00:00 2001 From: Emilianouz <135679131+Emilianouz@users.noreply.github.com> Date: Fri, 27 Feb 2026 16:39:18 +0000 Subject: [PATCH 1/6] Refactor algorithms for improved efficiency and Complexity. --- .../removeDuplicates/removeDuplicates.mjs | 31 +++++++------------ .../calculate_sum_and_product.py | 8 +++-- .../find_common_items/find_common_items.py | 15 +++++---- .../has_pair_with_sum/has_pair_with_sum.py | 14 ++++++--- 4 files changed, 35 insertions(+), 33 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..0715f3c 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,34 +1,27 @@ /** * 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: for each element in the input is compared against all previous unique elements: o(n2) + * Space Complexity: we store n elements it all elements are unique. + * Optimal Time Complexity: Each element is processed once O(1). * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed */ export function removeDuplicates(inputSequence) { + const seenItems = new Set(); const uniqueItems = []; for ( - let currentIndex = 0; - currentIndex < inputSequence.length; - currentIndex++ + let i = 0; + i < inputSequence.length; + i++ ) { - 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]); + const value = inputSequence[i]; + + if (!seenItems.has(value)) { + seenItems.add(value); + uniqueItems.push(value); } } 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 cfd5cfd..2c31591 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 @@ -13,19 +13,21 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: "product": 30 // 2 * 3 * 5 } Time Complexity: + There were two loops, one for each operation. Each has complexity O(n) + for both loops, complexity is O(2n) Space Complexity: + space is constant O(1) Optimal time complexity: + With only one loop for both operations reduce complexity to o(1) """ # Edge case: empty list if not input_numbers: return {"sum": 0, "product": 1} sum = 0 - for current_number in input_numbers: - sum += current_number - product = 1 for current_number in input_numbers: + sum += current_number product *= current_number return {"sum": sum, "product": product} 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 478e2ef..83e13f8 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -9,13 +9,16 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: - Space Complexity: - Optimal time complexity: + Time Complexity: for each element in first_sequence, is compared against every element in second_sequence. + Space Complexity: O(the number of unique common elements) + Optimal time complexity: improved using a set for fast lookups """ + second_set = set(second_sequence) + seen = set() 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) + if i in second_set and i not in seen: + seen.add(i) + common_items.append(i) + return common_items 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 fe2da51..d92b98f 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 @@ -8,11 +8,15 @@ 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. Time Complexity: - Space Complexity: + there are loops nested that check every pair O(n2) + Space Complexity: O(n) Optimal time complexity: + using a set to check numbers seen O(n) """ - for i in range(len(numbers)): - for j in range(i + 1, len(numbers)): - if numbers[i] + numbers[j] == target_sum: - return True + numbers_seen = set() + for num in numbers: # O(n) + required_num = target_sum - num + if required_num in numbers_seen: + return True + numbers_seen.add(num) return False From 54c12f0fb9d542acc0983489aa67d3b5b7b3d1e6 Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Tue, 3 Mar 2026 16:42:58 +0000 Subject: [PATCH 2/6] Refactor removeDuplicates function for simplicity --- .../removeDuplicates/removeDuplicates.mjs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index 0715f3c..f3cbfce 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -10,20 +10,10 @@ */ export function removeDuplicates(inputSequence) { const seenItems = new Set(); - const uniqueItems = []; - for ( - let i = 0; - i < inputSequence.length; - i++ - ) { - const value = inputSequence[i]; - - if (!seenItems.has(value)) { + for (const value of inputSequence) { seenItems.add(value); - uniqueItems.push(value); - } } - return uniqueItems; + return Array.from(seenItems); } From 2805870fad6db679bb53f9ebf0f7c804e75f9cc2 Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Tue, 3 Mar 2026 16:48:55 +0000 Subject: [PATCH 3/6] Fix time complexity comment in removeDuplicates.mjs Updated the optimal time complexity comment to reflect correct analysis. --- Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index f3cbfce..e772a10 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -3,7 +3,7 @@ * * Time Complexity: for each element in the input is compared against all previous unique elements: o(n2) * Space Complexity: we store n elements it all elements are unique. - * Optimal Time Complexity: Each element is processed once O(1). + * Optimal Time Complexity: O(n) — You must inspect each element at least once, and Set lookups are O(1). * * @param {Array} inputSequence - Sequence to remove duplicates from * @returns {Array} New sequence with duplicates removed From e55de56cf29457c345f82f73ff6b486e63cad3e0 Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Tue, 3 Mar 2026 16:55:04 +0000 Subject: [PATCH 4/6] Update time complexity comment for clarity Clarified optimal time complexity explanation in comments. --- .../calculate_sum_and_product/calculate_sum_and_product.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2c31591..d030d5a 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 @@ -18,7 +18,7 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: Space Complexity: space is constant O(1) Optimal time complexity: - With only one loop for both operations reduce complexity to o(1) + With one loop for both operations, must iterate through all n elements in the list, so the overall time complexity is O(n). """ # Edge case: empty list if not input_numbers: From 7ad8c1fe10fd8ed5416371cef8197637a2e719dd Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Tue, 3 Mar 2026 16:59:26 +0000 Subject: [PATCH 5/6] Update docstring for optimal time and space complexity Clarified optimal time and space complexity in docstring. --- Sprint-1/Python/find_common_items/find_common_items.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 83e13f8..42d5ef0 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -11,7 +11,7 @@ def find_common_items( Time Complexity: for each element in first_sequence, is compared against every element in second_sequence. Space Complexity: O(the number of unique common elements) - Optimal time complexity: improved using a set for fast lookups + Optimal time complexity: The time complexity is O(n + m) and the space complexity is O(n + m). """ second_set = set(second_sequence) seen = set() From 14db9b90dd37fab8c812fd92f3a06edbb6dab593 Mon Sep 17 00:00:00 2001 From: Emiliano Uruena Date: Tue, 3 Mar 2026 17:07:36 +0000 Subject: [PATCH 6/6] Refine complexity analysis in find_common_items function Updated time and space complexity explanations in docstring. --- Sprint-1/Python/find_common_items/find_common_items.py | 4 ++-- 1 file changed, 2 insertions(+), 2 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 42d5ef0..18921cf 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -9,8 +9,8 @@ def find_common_items( """ Find common items between two arrays. - Time Complexity: for each element in first_sequence, is compared against every element in second_sequence. - Space Complexity: O(the number of unique common elements) + Time Complexity: The time complexity is O(n + m) because we build a set from the second sequence in O(m) time and iterate through the first sequence in O(n) time. + Space Complexity: The space complexity is O(n + m) because we store up to all elements of the second sequence in a set and up to the common elements in additional storage. Optimal time complexity: The time complexity is O(n + m) and the space complexity is O(n + m). """ second_set = set(second_sequence)