This program is for removing duplicates from an array without using any library.
Algo: -
Program:1. import array(It is only used to form an array in python)2. Create a class that forms an array and provide a function to remove duplicates.3. Input size and elements of array.4. pick an element one by one and compare to rest of the elements of the array.5. Remove the repeated elements.6. Display the resulting array.
import array as arr
class RemDup(object):
def __init__(self, n, lst):
self.n = n
self.ar = arr.array('i',lst) #forming array using list
def removeDup(self): #removes duplicate from an array
i=0
while i < self.n:
j=i+1
while j < self.n:
if self.ar[j]==self.ar[i]:
self.ar.remove(self.ar[j])
self.n=self.n-1
j=j+1
i=i+1
if __name__=='__main__':
n = int(input('Enter the no. of elements in the array :'))
lst = []
print("Enter the elements of the array : ")
for i in range(n):
lst.append(int(input()))
obj = RemDup(n,lst)
print("The entered array = ",obj.ar)
obj.removeDup()
print("The resulting elements of the array are = ",obj.ar)
Output :Enter the no. of elements in the array :8 Enter the elements of the array : 1 2 1 3 3 4 5 5 The entered array = array('i', [1, 2, 1, 3, 3, 4, 5, 5]) The resulting elements of the array are = array('i', [2, 1, 3, 4, 5])
Comments