-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathremove_duplicates.py
More file actions
34 lines (27 loc) · 1.16 KB
/
remove_duplicates.py
File metadata and controls
34 lines (27 loc) · 1.16 KB
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
30
31
32
33
34
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²) Quadratic The outer loop runs n times, and for each value, the inner loop also runs up to n times.
# Space complexity:O(n) store n elements in worst possible case
# Optimal time complexity: O(n) can be improved useing set
# """
# 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
def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
unique_element= set() # for unique element
order_element =[] # to order maintain
for value in values:
if value not in unique_element:
unique_element.add(value)
order_element.append(value)
return order_element