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