-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Expand file tree
/
Copy pathremove_duplicates.py
More file actions
48 lines (35 loc) · 1.18 KB
/
remove_duplicates.py
File metadata and controls
48 lines (35 loc) · 1.18 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Remove Duplicates
Remove duplicate elements from an array while preserving the original order.
Handles both hashable and unhashable items.
Reference: https://en.wikipedia.org/wiki/Duplicate_code
Complexity:
Time: O(n) for hashable items / O(n^2) worst case for unhashable items
Space: O(n)
"""
from __future__ import annotations
from collections.abc import Hashable
from typing import Any
def remove_duplicates(array: list[Any]) -> list[Any]:
"""Remove duplicate elements from an array, preserving order.
Uses a set for O(1) lookups on hashable items and falls back to
linear search for unhashable items.
Args:
array: Input list potentially containing duplicates.
Returns:
New list with duplicates removed, original order preserved.
Examples:
>>> remove_duplicates([1, 1, 2, 2, 3])
[1, 2, 3]
"""
seen = set()
unique_array = []
for item in array:
if isinstance(item, Hashable):
if item not in seen:
seen.add(item)
unique_array.append(item)
else:
if item not in unique_array:
unique_array.append(item)
return unique_array