Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 loops, one for each operation. Each has complexity O(n)
for both loops, complexity is O(2n)
Copy link

Choose a reason for hiding this comment

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

Note: Constant multiplier is ignored in complexity analysis. O(2n) and O(n) means the same thing.

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) because we build a set from the second sequence in O(m) time and iterate through the first sequence in O(n) time.
Copy link

Choose a reason for hiding this comment

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

This seems like a complexity analysis for the refactored code. The original code does not use any set.

What's the complexity of the original code?

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)
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