Skip to content

Commit b7a255f

Browse files
committed
use set to remove duplicates
1 parent b5e1e07 commit b7a255f

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,28 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
88
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
99
1010
Time complexity:
11+
ANSWER: O(n)
12+
Because the sequence is looped through once.
13+
1114
Space complexity:
15+
ANSWER: O(n)
16+
Because a set is used to track seen value
17+
18+
1219
Optimal time complexity:
20+
ANSWER: O(n)
21+
Each value is processed once using a set for faster lookup
22+
1323
"""
1424
unique_items = []
25+
seen = set()
26+
1527

1628
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:
29+
if value not in seen:
30+
seen.add(value)
31+
2332
unique_items.append(value)
33+
2434

2535
return unique_items

0 commit comments

Comments
 (0)