File tree Expand file tree Collapse file tree
Sprint-1/Python/remove_duplicates Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33ItemType = 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.
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.
99
10- Time complexity:
11- Space complexity:
12- Optimal time complexity:
13- """
14- unique_items = []
10+ # Time complexity:O(n²) Quadratic The outer loop runs n times, and for each value, the inner loop also runs up to n times.
11+ # Space complexity:O(n) store n elements in worst possible case
12+ # Optimal time complexity: O(n) can be improved useing set
13+ # """
14+ # unique_items = []
1515
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 )
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
2426
25- return unique_items
27+ def remove_duplicates (values : Sequence [ItemType ]) -> List [ItemType ]:
28+ unique_element = set () # for unique element
29+ order_element = [] # to order maintain
30+ for value in values :
31+ if value not in unique_element :
32+ unique_element .add (value )
33+ order_element .append (value )
34+ return order_element
You can’t perform that action at this time.
0 commit comments