Python Program to remove duplicates from Array

In this tutorial, you will learn writing program for how to remove duplicates from an array Python.

To remove duplicate elements from an array first we will count the occurrence of all elements.

After finding occurrence we will print elements only one time and can get element after removing duplicates.

Also Read This : Java program to remove duplicate elements from an array.

Python Program to remove duplicate elements from array

arr = []
n = int(input("Enter size of array: "))
for _ in range(n):
    val = int(input("Enter element of array: "))
    arr.append(val)

print("Array elements after removing duplicates:")

seen = set()
for val in arr:
    if val not in seen:
        print(val)
        seen.add(val)

Output

Enter size of array: 6
Enter element of array: 5
Enter element of array: 5
Enter element of array: 4
Enter element of array: 3
Enter element of array: 3
Enter element of array: 1
Array elements after removing duplicates:
5
4
3
1
What did you think?

Similar Reads

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now