From 73b83b21e5b3ebd4e2e48028a29de26044ed6b05 Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Tue, 30 Jun 2026 22:45:31 +0100 Subject: [PATCH 1/3] Refactor: optimize the js and py scripts to optimize time and space complexity. --- .../calculateSumAndProduct.js | 12 +++---- .../findCommonItems/findCommonItems.js | 19 +++++++---- .../hasPairWithSum/hasPairWithSum.js | 21 +++++++----- .../removeDuplicates/removeDuplicates.mjs | 33 ++++--------------- .../calculate_sum_and_product.py | 13 ++++---- .../find_common_items/find_common_items.py | 21 +++++++----- .../has_pair_with_sum/has_pair_with_sum.py | 17 ++++++---- .../remove_duplicates/remove_duplicates.py | 22 +++++-------- 8 files changed, 74 insertions(+), 84 deletions(-) diff --git a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js index ce738c3..fdca9be 100644 --- a/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js +++ b/Sprint-1/JavaScript/calculateSumAndProduct/calculateSumAndProduct.js @@ -9,21 +9,18 @@ * "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 */ 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; } @@ -32,3 +29,4 @@ export function calculateSumAndProduct(numbers) { product: product, }; } +console.log(calculateSumAndProduct([1, 2, 3])); diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index 5619ae5..eb1e715 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,14 +1,21 @@ /** * Finds common items between two arrays. * - * Time Complexity: - * Space Complexity: - * Optimal Time Complexity: + * Time Complexity:O(1) + * Space Complexity:O(n+m) + * Optimal Time Complexity:O(1) * * @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 secondArr = new Set(secondArray); + + const uniqueValues = [ + ...new Set(firstArray.filter((item) => secondArr.has(item))), + ]; + return uniqueValues; +}; + +console.log(findCommonItems([2, 5, 7], [5, 6, 7])); diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index dd2901f..c3fbf97 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,21 +1,26 @@ /** * 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(n) + * Optimal Time Complexity:O(n) * * @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 seen = new Set(); + + for (let num of numbers) { + const complement = target - num; + console.log(complement); + if (seen.has(complement)) { + return true; } + seen.add(num); } return false; } + +console.log(hasPairWithSum([1, 2, 3], 4)); diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index dc5f771..e67193d 100644 --- a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs +++ b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs @@ -1,36 +1,15 @@ /** * 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 */ 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)]; } + +console.log(removeDuplicates([3, 3, 5, 6, 3, 7])); 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..cacb323 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,20 +12,21 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: "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) """ # 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} + +print(calculate_sum_and_product([1,2,3,4])) 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..2dddd7a 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: O(n+m) + Space Complexity:O(n+m) + Optimal time complexity: O(n+m) """ - 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) - return common_items + + first_set = set(first_sequence) + second_set = set(second_sequence) + # we use the & operator shortcut, of The intersection() method + # to return a set that contains the similarity between two or more sets + common_items= first_set & second_set + return list(common_items) + +print(find_common_items([1,3,5,4],[1,4,8,0])) 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..ab5e418 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 @@ -7,12 +7,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: - Optimal time complexity: + Time Complexity:O(n) + Space Complexity:O(n) + Optimal time complexity: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 + seen = set() + for num in numbers: + complement = target_sum - num + if complement in seen: + return True + seen.add(num) return False +print(has_pair_with_sum([1,2,3,4],3)) diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index c9fdbe8..9645d12 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,19 +7,13 @@ 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: + Time complexity:O(n) + Space complexity:O(n) + Optimal time complexity:O(n) """ - 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 + + # dict.fromkeys creates a dictionary with values as keys,and automatically remove duplicates + #list() to keep the original order. + return list(dict.fromkeys(values)) +print(remove_duplicates([1,2,2,3,3,4,0,0])) From dbc17d1614c4f8a6f09e6f7b585db91074b5f2bc Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Wed, 1 Jul 2026 11:49:15 +0100 Subject: [PATCH 2/3] fix: resolve the reviwer comments --- Sprint-1/JavaScript/findCommonItems/findCommonItems.js | 10 +++++----- Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js | 5 ++--- .../JavaScript/removeDuplicates/removeDuplicates.mjs | 2 +- Sprint-1/Python/find_common_items/find_common_items.py | 4 ++-- Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py | 4 ++-- Sprint-1/Python/remove_duplicates/remove_duplicates.py | 2 +- 6 files changed, 13 insertions(+), 14 deletions(-) diff --git a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js index eb1e715..9035713 100644 --- a/Sprint-1/JavaScript/findCommonItems/findCommonItems.js +++ b/Sprint-1/JavaScript/findCommonItems/findCommonItems.js @@ -1,19 +1,19 @@ /** * Finds common items between two arrays. * - * Time Complexity:O(1) - * Space Complexity:O(n+m) - * Optimal Time Complexity:O(1) + * Time Complexity:O(n*m) + * Space Complexity:O(u) + * 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) => { - const secondArr = new Set(secondArray); + const allowedItems = new Set(secondArray); const uniqueValues = [ - ...new Set(firstArray.filter((item) => secondArr.has(item))), + ...new Set(firstArray.filter((item) => allowedItems.has(item))), ]; return uniqueValues; }; diff --git a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js index c3fbf97..7922fa9 100644 --- a/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js +++ b/Sprint-1/JavaScript/hasPairWithSum/hasPairWithSum.js @@ -1,8 +1,8 @@ /** * Find if there is a pair of numbers that sum to a given target value. * - * Time Complexity:O(n) - * Space Complexity:O(n) + * Time Complexity:O(n^2) + * Space Complexity:O(1) * Optimal Time Complexity:O(n) * * @param {Array} numbers - Array of numbers to search through @@ -14,7 +14,6 @@ export function hasPairWithSum(numbers, target) { for (let num of numbers) { const complement = target - num; - console.log(complement); if (seen.has(complement)) { return true; } diff --git a/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs b/Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs index e67193d..cfe282a 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^2) * Space Complexity:O(n) * Optimal Time Complexity:O(n) * 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 2dddd7a..f5bb889 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: O(n+m) - Space Complexity:O(n+m) + Time Complexity: O(n^3) + Space Complexity:O(u) Optimal time complexity: O(n+m) """ 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 ab5e418..8cdf348 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 @@ -7,8 +7,8 @@ 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:O(n) - Space Complexity:O(n) + Time Complexity:O(n^2) + Space Complexity:O(1) Optimal time complexity:O(n) """ seen = set() diff --git a/Sprint-1/Python/remove_duplicates/remove_duplicates.py b/Sprint-1/Python/remove_duplicates/remove_duplicates.py index 9645d12..2b4dde2 100644 --- a/Sprint-1/Python/remove_duplicates/remove_duplicates.py +++ b/Sprint-1/Python/remove_duplicates/remove_duplicates.py @@ -7,7 +7,7 @@ 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:O(n) + Time complexity:O(n^2) Space complexity:O(n) Optimal time complexity:O(n) """ From 35d4d4f555ee7c23a72c65002ff34736268a5d3f Mon Sep 17 00:00:00 2001 From: Jamal Laqdiem Date: Thu, 2 Jul 2026 10:35:30 +0100 Subject: [PATCH 3/3] fix the space complex --- 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 f5bb889..93271a2 100644 --- a/Sprint-1/Python/find_common_items/find_common_items.py +++ b/Sprint-1/Python/find_common_items/find_common_items.py @@ -10,7 +10,7 @@ def find_common_items( Find common items between two arrays. Time Complexity: O(n^3) - Space Complexity:O(u) + Space Complexity:O(min(n,m)) Optimal time complexity: O(n+m) """