Skip to content
31 changes: 7 additions & 24 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
/**
* 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: 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
*/
export function removeDuplicates(inputSequence) {
const uniqueItems = [];
const seenItems = new Set();

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]);
}
for (const value of inputSequence) {
seenItems.add(value);
}

return uniqueItems;
return Array.from(seenItems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 separated loops, one for each operation. Each had complexity O(n)
The refactor combined them into a single loop reducing the constant factor, maintaining the same Big-O complexity.
Space Complexity:
space is constant O(1)
Optimal time complexity:
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:
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}
15 changes: 9 additions & 6 deletions Sprint-1/Python/find_common_items/find_common_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ def find_common_items(
"""
Find common items between two arrays.

Time Complexity:
Space Complexity:
Optimal time complexity:
Time Complexity: The time complexity is O(n + m)
Space Complexity: The space complexity is O(n + m)
Optimal time complexity: The time complexity is O(n + m)
Comment on lines +12 to +14
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the time complexity of the the original code?

Can you explan why your implementation has a space and time complexity of O(n+m)?

"""
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
14 changes: 9 additions & 5 deletions Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading