We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5903a04 commit 56b124eCopy full SHA for 56b124e
1 file changed
Sprint-1/Python/remove_duplicates/remove_duplicates.py
@@ -23,3 +23,33 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
23
unique_items.append(value)
24
25
return unique_items
26
+
27
28
+"""
29
+Time Complexity:
30
31
+1 + 2 + 3 + ... + (n - 1) = n(n - 1)/2 = O(n²)
32
33
+Space Complexity:
34
35
+Even though we're just using .append(), the list grows linearly, so it is s O(n) space.
36
37
38
39
+#Optimal Solution
40
41
+def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]:
42
+ seen = set()
43
+ unique_items = []
44
45
+ for value in values:
46
+ if value not in seen:
47
+ seen.add(value)
48
+ unique_items.append(value)
49
50
+ return unique_items
51
52
+'''
53
+Time Complexity: O(n)
54
+Space Complexity: O(n) storing unique_items and seen
55
0 commit comments