File tree Expand file tree Collapse file tree 1 file changed +11
-13
lines changed
Sprint-1/Python/remove_duplicates Expand file tree Collapse file tree 1 file changed +11
-13
lines changed Original file line number Diff line number Diff line change 66def 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
You can’t perform that action at this time.
0 commit comments