Skip to content

Commit f721493

Browse files
committed
Refactor algorithms for improved efficiency and Complexity.
1 parent e718fb4 commit f721493

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
/**
22
* Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
33
*
4-
* Time Complexity:
5-
* Space Complexity:
6-
* Optimal Time Complexity:
4+
* Time Complexity: for each element in the input is compared against all previous unique elements: o(n2)
5+
* Space Complexity: we store n elements it all elements are unique.
6+
* Optimal Time Complexity: Each element is processed once O(1).
77
*
88
* @param {Array} inputSequence - Sequence to remove duplicates from
99
* @returns {Array} New sequence with duplicates removed
1010
*/
1111
export function removeDuplicates(inputSequence) {
12+
const seenItems = new Set();
1213
const uniqueItems = [];
1314

1415
for (
15-
let currentIndex = 0;
16-
currentIndex < inputSequence.length;
17-
currentIndex++
16+
let i = 0;
17+
i < inputSequence.length;
18+
i++
1819
) {
19-
let isDuplicate = false;
20-
for (
21-
let compareIndex = 0;
22-
compareIndex < uniqueItems.length;
23-
compareIndex++
24-
) {
25-
if (inputSequence[currentIndex] === uniqueItems[compareIndex]) {
26-
isDuplicate = true;
27-
break;
28-
}
29-
}
30-
if (!isDuplicate) {
31-
uniqueItems.push(inputSequence[currentIndex]);
20+
const value = inputSequence[i];
21+
22+
if (!seenItems.has(value)) {
23+
seenItems.add(value);
24+
uniqueItems.push(value);
3225
}
3326
}
3427

Sprint-1/Python/calculate_sum_and_product/calculate_sum_and_product.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]:
1313
"product": 30 // 2 * 3 * 5
1414
}
1515
Time Complexity:
16+
There were two loops, one for each operation. Each has complexity O(n)
17+
for both loops, complexity is O(2n)
1618
Space Complexity:
19+
space is constant O(1)
1720
Optimal time complexity:
21+
With only one loop for both operations reduce complexity to o(1)
1822
"""
1923
# Edge case: empty list
2024
if not input_numbers:
2125
return {"sum": 0, "product": 1}
2226

2327
sum = 0
24-
for current_number in input_numbers:
25-
sum += current_number
26-
2728
product = 1
2829
for current_number in input_numbers:
30+
sum += current_number
2931
product *= current_number
3032

3133
return {"sum": sum, "product": product}

Sprint-1/Python/find_common_items/find_common_items.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ def find_common_items(
99
"""
1010
Find common items between two arrays.
1111
12-
Time Complexity:
13-
Space Complexity:
14-
Optimal time complexity:
12+
Time Complexity: for each element in first_sequence, is compared against every element in second_sequence.
13+
Space Complexity: O(the number of unique common elements)
14+
Optimal time complexity: improved using a set for fast lookups
1515
"""
16+
second_set = set(second_sequence)
17+
seen = set()
1618
common_items: List[ItemType] = []
1719
for i in first_sequence:
18-
for j in second_sequence:
19-
if i == j and i not in common_items:
20-
common_items.append(i)
20+
if i in second_set and i not in seen:
21+
seen.add(i)
22+
common_items.append(i)
23+
2124
return common_items

Sprint-1/Python/has_pair_with_sum/has_pair_with_sum.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool:
88
Find if there is a pair of numbers that sum to a target value.
99
1010
Time Complexity:
11-
Space Complexity:
11+
there are loops nested that check every pair O(n2)
12+
Space Complexity: O(n)
1213
Optimal time complexity:
14+
using a set to check numbers seen O(n)
1315
"""
14-
for i in range(len(numbers)):
15-
for j in range(i + 1, len(numbers)):
16-
if numbers[i] + numbers[j] == target_sum:
17-
return True
16+
numbers_seen = set()
17+
for num in numbers: # O(n)
18+
required_num = target_sum - num
19+
if required_num in numbers_seen:
20+
return True
21+
numbers_seen.add(num)
1822
return False

0 commit comments

Comments
 (0)