Removing Duplicates elements from an array

remove duplicate elements from an array

Remove duplicate elements in Python

Here, in this section we will discuss the program to remove duplicate elements in python programming language. We will discuss two approaches to solve this program, by using extra space and without using extra space.

Here, we will discuss the following two methods, for removing the duplicate elements from the given array.

  • Method 1 : Using Auxiliary space.
  • Method 2 : Without using extra space

Now, let’s discuss the above two methods in brief,

Method 1 :

  • Declare an array say temp[], as an auxiliary array.
  • Traverse input array and one by one copy unique elements of arr[] to temp[]. Also keep track of count of unique elements. Let this count be j
  • Copy j elements from temp[] to arr[] and return j.

Time and Space Complexity :

  • Time Complexity : O(n)
  • Space Complexity : O(n)

Method 1 : Code in Python

Run
# This function returns new size of modified array.
def removeDuplicates(arr, n):
# Return, if array is empty or contains a single element
    if n == 0 or n == 1:
        return n

    temp = list(range(n))

    # Start traversing elements
    j = 0;
    for i in range(0, n-1):
        # If current element is not equal to next element then store that current element
        if arr[i] != arr[i+1]:
            temp[j] = arr[i]
            j += 1

    temp[j] = arr[n-1]
    j += 1

    # Modify original array
    for i in range(0, j):
        arr[i] = temp[i]

    return j

# Driver code
arr = [10, 20, 20, 30, 40, 40, 40, 50, 50]
n = len(arr)

n = removeDuplicates(arr, n)

# Print updated array
for i in range(n):
    print ("%d"%(arr[i]), end = " ")

Output

10 20 30 40 50

Method 2 (Efficient Approach) :

In this method we will maintain a separate index for same array as maintained for different array in Method 1

Method 2 : Code in Python

Run
# This function returns new size of modified array.
def removeDuplicates(arr, n):

    # Return, if array is empty or contains a single element
    if n == 0 or n == 1:
        return n

    # Start traversing elements
    j = 0;
    for i in range(0, n-1):

        # If current element is not equal to next element then store that current element
        if arr[i] != arr[i+1]:
            arr[j] = arr[i]
            j += 1

    arr[j] = arr[n-1]
    j += 1

    return j

# Driver code
arr = [10, 20, 20, 30, 40, 40, 40, 50, 50]
n = len(arr)

n = removeDuplicates(arr, n)

# Print updated array
for i in range(n):
    print ("%d"%(arr[i]), end = " ")

Output

10 20 30 40 50

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

4 comments on “Removing Duplicates elements from an array”


  • SOMNATH GHOSH

    def count(arr):
    s=dict()
    for i in range(len(arr)):
    if arr[i] in s.keys():
    s[arr[i]]+=1
    else:
    s[arr[i]]=1
    return s
    # Driver Code
    arr = [10, 30, 40, 20, 10, 20, 50, 10]
    list(count(arr))


  • Mudit

    arr = [1,2,44,3,44,55,55,55]
    temp = []
    for i in range(len(arr)):
    if arr[i] in temp:
    continue
    else:
    temp.append(arr[i])

    print(temp)


  • Pratik

    arr=[10,20,30,40,10,20,60,40,20,20]
    new=[]
    for i in arr:
    if i not in new:
    new.append(i)
    print(new)