Skip to content

Commit 71bb632

Browse files
committed
refactor: remove duplicates while preserving order
1 parent 216b403 commit 71bb632

1 file changed

Lines changed: 31 additions & 20 deletions

File tree

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

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,34 @@
33
ItemType = TypeVar("ItemType")
44

55

6-
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
7-
"""
8-
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
9-
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
13-
"""
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
6+
# def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
7+
# """
8+
# Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
9+
10+
# Time complexity:
11+
# Space complexity:
12+
# Optimal time complexity:
13+
# """
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
26+
27+
def remove_duplicates(values):
28+
seen = set()
29+
result = []
30+
31+
for item in values:
32+
if item not in seen:
33+
seen.add(item)
34+
result.append(item)
35+
36+
return result

0 commit comments

Comments
 (0)