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: 12 additions & 19 deletions Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
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)
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}
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: 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
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