Duplicate Elements Removal of an Array or List using Python 3.6

Duplicate Elements Removal of an Array in Python

Duplicate Elements Removal:

  • The user inputs integer elements of an array randomly i.e., without counting.
  • The program should output distinct elements of the array.

The image below shows the flow chart of checking of duplicate elements in an array or list.

remove duplicate elements from an array or list in Python

The following is the code snippet with their respective outputs.

PROGRAM 1:

print("Enter Array Elements:")     #using set()
l=list(map(int,input().split()))
l=set(l)
l=list(l)
for i in range(len(l)):
    print(l[i],end=" ")

OUTPUT 1:

Enter Array Elements:
4 5 8 5 4 6 2 1 5 6 3 5 9
1 2 3 4 5 6 8 9

OUTPUT 2:



Enter Array Elements:
4 7 8 2 5 6 4 2 6 7 8 5 2 4 6 2 4 8 3 2 1 4 5 6
1 2 3 4 5 6 7 8

PROGRAM 2:

print("Input Array Elements:")       #not using set()
a=list(map(int,input().split()))
b=[]
for num in a:
    if num not in b:
        b.append(num)
for i in range(len(b)):
    print(b[i],end=" ")

OUTPUT 3:

Input Array Elements:
4 5 7 8 5 4 4 6 3 2 1
4 5 7 8 6 3 2 1

OUTPUT 4:

Input Array Elements:
6 9 5 2 4 5 2 1 4 7 8 9 9 3 6 4 5 2 2 4 5 5
6 9 5 2 4 1 7 8 3

So, the following are some points to be noted by comparing Programs 1 and 2.

  • Outputs 1 and 2 are the results of Program 1.
  • Outputs 3 and 4 are the results of Program 2.
  • There’s a slight difference in the Outputs of Programs 1 and 2.
  • The difference is because of the application of set(), which is an¬†inbuilt function used in Program 1 but not in Program 2.
  • Program 1 outputs in Ascending Order whereas Program 2 doesn’t.

Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *