0
0
PythonProgramBeginner · 2 min read

Python Program to Remove Duplicates from Array

You can remove duplicates from an array in Python by converting it to a set and back to a list using list(set(array)) which keeps only unique elements.
📋

Examples

Input[1, 2, 2, 3, 4, 4, 5]
Output[1, 2, 3, 4, 5]
Input[7, 7, 7, 7]
Output[7]
Input[]
Output[]
🧠

How to Think About It

To remove duplicates, think of the array as a collection of items where some repeat. We want to keep only one copy of each item. Using a set helps because it automatically ignores repeated items. Then we convert back to a list to keep the array form.
📐

Algorithm

1
Take the input array.
2
Convert the array to a set to remove duplicates.
3
Convert the set back to a list to restore array format.
4
Return or print the new list without duplicates.
💻

Code

python
def remove_duplicates(arr):
    return list(set(arr))

# Example usage
array = [1, 2, 2, 3, 4, 4, 5]
print(remove_duplicates(array))
Output
[1, 2, 3, 4, 5]
🔍

Dry Run

Let's trace the example array [1, 2, 2, 3, 4, 4, 5] through the code

1

Input array

arr = [1, 2, 2, 3, 4, 4, 5]

2

Convert to set

set(arr) = {1, 2, 3, 4, 5} (duplicates removed)

3

Convert set back to list

list({1, 2, 3, 4, 5}) = [1, 2, 3, 4, 5]

4

Return result

[1, 2, 3, 4, 5]

StepSet ContentList Result
InitialN/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

Using a loop and a new list
python
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]))
This method preserves the original order but is slower for large arrays.
Using dict.fromkeys()
python
def remove_duplicates_dict(arr):
    return list(dict.fromkeys(arr))

print(remove_duplicates_dict([1, 2, 2, 3, 4, 4, 5]))
This method removes duplicates and preserves order using dictionary keys.

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.

ApproachTimeSpaceBest For
set()O(n)O(n)Fast removal, order not preserved
Loop with listO(n²)O(n)Preserving order, small arrays
dict.fromkeys()O(n)O(n)Preserving order, efficient
💡
Use list(set(array)) for a quick way to remove duplicates but note it does not keep the original order.
⚠️
Beginners often forget that converting to a set loses the original order of elements.