Python Program to Remove Duplicates from Array
list(set(array)) which keeps only unique elements.Examples
How to Think About It
Algorithm
Code
def remove_duplicates(arr): return list(set(arr)) # Example usage array = [1, 2, 2, 3, 4, 4, 5] print(remove_duplicates(array))
Dry Run
Let's trace the example array [1, 2, 2, 3, 4, 4, 5] through the code
Input array
arr = [1, 2, 2, 3, 4, 4, 5]
Convert to set
set(arr) = {1, 2, 3, 4, 5} (duplicates removed)
Convert set back to list
list({1, 2, 3, 4, 5}) = [1, 2, 3, 4, 5]
Return result
[1, 2, 3, 4, 5]
| Step | Set Content | List Result |
|---|---|---|
| Initial | N/A | [1, 2, 2, 3, 4, 4, 5] |
| After set() | {1, 2, 3, 4, 5} | N/A |
| After list() | N/A | [1, 2, 3, 4, 5] |
Why This Works
Step 1: Using a set removes duplicates
A set in Python automatically keeps only unique items, so converting the array to a set removes repeated elements.
Step 2: Convert back to list for array format
Since sets are unordered collections, converting back to a list gives us a familiar array structure to work with.
Step 3: Return the new list
The final list contains only unique elements from the original array, effectively removing duplicates.
Alternative Approaches
def remove_duplicates_loop(arr): result = [] for item in arr: if item not in result: result.append(item) return result print(remove_duplicates_loop([1, 2, 2, 3, 4, 4, 5]))
def remove_duplicates_dict(arr): return list(dict.fromkeys(arr)) print(remove_duplicates_dict([1, 2, 2, 3, 4, 4, 5]))
Complexity: O(n) time, O(n) space
Time Complexity
Converting the list to a set takes O(n) time because it processes each element once. Converting back to a list also takes O(n). Overall, the time is O(n).
Space Complexity
A new set and list are created to hold unique elements, so extra space proportional to the number of unique elements is used, O(n).
Which Approach is Fastest?
Using set() is fastest for removing duplicates but does not preserve order. Using a loop or dict.fromkeys() preserves order but can be slower.
| Approach | Time | Space | Best For |
|---|---|---|---|
| set() | O(n) | O(n) | Fast removal, order not preserved |
| Loop with list | O(n²) | O(n) | Preserving order, small arrays |
| dict.fromkeys() | O(n) | O(n) | Preserving order, efficient |
list(set(array)) for a quick way to remove duplicates but note it does not keep the original order.