Skip to content

Commit 5546056

Browse files
committed
completed removeDuplicates
1 parent 0d67520 commit 5546056

2 files changed

Lines changed: 51 additions & 38 deletions

File tree

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
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: O(N^2). It takes a loop inside a loop to compare duplicates.
5+
* Space Complexity: O(N). The uniqueItems array would grow to N in the worst case.
6+
* Optimal Time Complexity: O(N). It only loops once after optimization.
77
*
88
* @param {Array} inputSequence - Sequence to remove duplicates from
99
* @returns {Array} New sequence with duplicates removed
1010
*/
11-
export function removeDuplicates(inputSequence) {
12-
const uniqueItems = [];
11+
// export function removeDuplicates(inputSequence) {
12+
// const uniqueItems = [];
13+
14+
// for (
15+
// let currentIndex = 0;
16+
// currentIndex < inputSequence.length;
17+
// currentIndex++
18+
// ) {
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]);
32+
// }
33+
// }
1334

14-
for (
15-
let currentIndex = 0;
16-
currentIndex < inputSequence.length;
17-
currentIndex++
18-
) {
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]);
32-
}
33-
}
35+
// return uniqueItems;
36+
// }
3437

35-
return uniqueItems;
36-
}
38+
export function removeDuplicates(inputSequence) {
39+
return [...new Set(inputSequence)];
40+
}

‎Sprint-1/Python/remove_duplicates/remove_duplicates.py‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,28 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
77
"""
88
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
99
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
10+
Time complexity: O(N^2). It takes a loop inside a loop
11+
Space complexity: O(N). The space of unique_items would grow to N in the worst case.
12+
Optimal time complexity: O(N). It only loops once after optimization, but the space becomes O(N + N)
1313
"""
14-
unique_items = []
14+
# unique_items = []
15+
16+
# for value in values:
17+
# is_duplicate = False
18+
# for existing in unique_items:
19+
# if value == existing:
20+
# is_duplicate = True
21+
# break
22+
# if not is_duplicate:
23+
# unique_items.append(value)
24+
25+
# return unique_items
1526

27+
seen = set()
28+
unique_items = []
1629
for value in values:
17-
is_duplicate = False
18-
for existing in unique_items:
19-
if value == existing:
20-
is_duplicate = True
21-
break
22-
if not is_duplicate:
30+
if value not in seen:
31+
seen.add(value)
2332
unique_items.append(value)
24-
33+
2534
return unique_items

0 commit comments

Comments
 (0)