Skip to content

Commit 655c493

Browse files
committed
remove duplicates
1 parent b99ce99 commit 655c493

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

Sprint-1/Python/remove_duplicates/remove_duplicates.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
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.
9-
10-
Time complexity:
11-
Space complexity:
12-
Optimal time complexity:
9+
Outer loop runs n times (each value in values)
10+
Inner loop runs up to k times (size of unique_items, worst case k = n)
11+
Time complexity: O(n^2)
12+
Space complexity: O(n)
13+
Optimal time complexity: O(n)
1314
"""
14-
unique_items = []
15+
seen = set()
16+
result = []
1517

1618
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)
19+
if value not in seen:
20+
seen.add(value)
21+
result.append(value)
2422

25-
return unique_items
23+
return result

0 commit comments

Comments
 (0)