-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathremove_duplicates.py
More file actions
29 lines (22 loc) · 876 Bytes
/
remove_duplicates.py
File metadata and controls
29 lines (22 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from typing import List, Sequence, TypeVar
ItemType = TypeVar("ItemType")
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
"""
Remove duplicate values from a sequence, preserving the order of the first occurrence of each value.
Time complexity: O(n^2)
Space complexity: O(n)
Optimal time complexity: 0(n)
"""
# unique_items = []
# for value in values:
# is_duplicate = False
# for existing in unique_items:
# if value == existing:
# is_duplicate = True
# break
# if not is_duplicate:
# unique_items.append(value)
# return unique_items
# nested array - O(n^2) complexity
# here we iterate through the array to make a dict which is O(n) and than convert it to the list which is also O(n)
return list(dict.fromkeys(values))