Skip to content
Related Articles

Related Articles

Remove duplicates from sorted array

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 12 Jul, 2022
View Discussion
Improve Article
Save Article

Given a sorted array, the task is to remove the duplicate elements from the array.

Examples: 

Input  : arr[] = {2, 2, 2, 2, 2}
Output : arr[] = {2}
         new size = 1

Input  : arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}
Output : arr[] = {1, 2, 3, 4, 5}
         new size = 5

Method 1: (Using extra space) 

  1. Create an auxiliary array temp[] to store unique elements.
  2. 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.
  3. Copy j elements from temp[] to arr[] and return j
 

Complete Interview Preparation - GFGImplementation:

C++




// Simple C++ program to remove duplicates
#include <iostream>
using namespace std;
 
// Function to remove duplicate elements This function
// returns new size of modified array.
int removeDuplicates(int arr[], int n)
{
    // Return, if array is empty or contains a single
    // element
    if (n == 0 || n == 1)
        return n;
 
    int temp[n];
 
    // Start traversing elements
    int j = 0;
    // If current element is not equal to next element
    // then store that current element
    for (int i = 0; i < n - 1; i++)
        if (arr[i] != arr[i + 1])
            temp[j++] = arr[i];
 
    // Store the last element as whether it is unique or
    // repeated, it hasn't stored previously
    temp[j++] = arr[n - 1];
 
    // Modify original array
    for (int i = 0; i < j; i++)
        arr[i] = temp[i];
 
    return j;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 3, 4, 4, 4, 5, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // removeDuplicates() returns new size of array.
    n = removeDuplicates(arr, n);
 
    // Print updated array
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C




// Simple C program to remove duplicates
#include <stdio.h>
 
// Function to remove duplicate elements This function
// returns new size of modified array.
int removeDuplicates(int arr[], int n)
{
    // Return, if array is empty or contains a single
    // element
    if (n == 0 || n == 1)
        return n;
 
    int temp[n];
 
    // Start traversing elements
    int j = 0;
    // If current element is not equal to next element
    // then store that current element
    for (int i = 0; i < n - 1; i++)
        if (arr[i] != arr[i + 1])
            temp[j++] = arr[i];
 
    // Store the last element as whether it is unique or
    // repeated, it hasn't stored previously
    temp[j++] = arr[n - 1];
 
    // Modify original array
    for (int i = 0; i < j; i++)
        arr[i] = temp[i];
 
    return j;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 2, 3, 4, 4, 4, 5, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // removeDuplicates() returns new size of array.
    n = removeDuplicates(arr, n);
 
    // Print updated array
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java




// simple java program to remove duplicates
 
class Main {
    // Function to remove duplicate elements This function
    // returns new size of modified array.
    static int removeDuplicates(int arr[], int n)
    {
        // Return, if array is empty or contains a single
        // element
        if (n == 0 || n == 1)
            return n;
 
        int[] temp = new int[n];
 
        // Start traversing elements
        int j = 0;
        for (int i = 0; i < n - 1; i++)
            // If current element is not equal to next
            // element then store that current element
            if (arr[i] != arr[i + 1])
                temp[j++] = arr[i];
 
        // Store the last element as whether it is unique or
        // repeated, it hasn't stored previously
        temp[j++] = arr[n - 1];
 
        // Modify original array
        for (int i = 0; i < j; i++)
            arr[i] = temp[i];
 
        return j;
    }
 
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 2, 3, 4, 4, 4, 5, 5 };
        int n = arr.length;
 
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3




# Python3 program to
# remove duplicates
# Function to remove
# duplicate elements
 
# 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
 
    # Store the last element
    # as whether it is unique
    # or repeated, it hasn't
    # stored previously
    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 = [1, 2, 2, 3, 4, 4, 4, 5, 5]
n = len(arr)
 
# removeDuplicates() returns
# new size of array.
n = removeDuplicates(arr, n)
 
# Print updated array
for i in range(n):
    print ("%d"%(arr[i]), end = " ")

C#




// Simple C# program to remove
// duplicates
using System;
 
class GFG {
     
    // Function to remove duplicate
    // elements This function returns
    // new size of modified array.
    static int removeDuplicates(int []arr, int n)
    {
         
        // Return, if array is empty
        // or contains a single element
        if (n == 0 || n == 1)
            return n;
     
        int []temp = new int[n];
         
        // Start traversing elements
        int j = 0;
         
        for (int i = 0; i < n - 1; i++)
         
            // If current element is not equal
            // to next element then store that
            // current element
            if (arr[i] != arr[i+1])
                temp[j++] = arr[i];
         
        // Store the last element as
        // whether it is unique or
        // repeated, it hasn't
        // stored previously
        temp[j++] = arr[n-1];
         
        // Modify original array
        for (int i = 0; i < j; i++)
            arr[i] = temp[i];
     
        return j;
    }
     
    public static void Main ()
    {
        int []arr = {1, 2, 2, 3, 4, 4, 4, 5, 5};
        int n = arr.Length;
         
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by nitin mittal.

Javascript




<script>
 
// Simple JavaScript program to remove
// duplicates
 
// Function to remove duplicate elements
// This function returns new size of modified
// array.
function removeDuplicates(arr, n)
{
    // Return, if array is empty
    // or contains a single element
    if (n==0 || n==1)
        return n;
 
    var temp = new Array(n);
 
    // Start traversing elements
    var j = 0;
    for (var i=0; i<n-1; i++)
 
        // If current element is not equal
        // to next element then store that
        // current element
        if (arr[i] != arr[i+1])
            temp[j++] = arr[i];
 
    // Store the last element as whether
    // it is unique or repeated, it hasn't
    // stored previously
    temp[j++] = arr[n-1];
 
    // Modify original array
    for (var i=0; i<j; i++)
        arr[i] = temp[i];
 
    return j;
}
 
var arr = [1, 2, 2, 3, 4, 4, 4, 5, 5];
    var n = arr.length;
 
    // removeDuplicates() returns new size of
    // array.
    n = removeDuplicates(arr, n);
 
    // Print updated array
    for (var i=0; i<n; i++)
       document.write( arr[i]+" ");
 
// This code is contributed by SoumikMondal
 
</script>

Output

1 2 3 4 5 

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

Method 2: (Constant extra space)

Just maintain a separate index for same array as maintained for different array in Method 1.

Implementation:

C++




// C++ program to remove duplicates in-place
#include<iostream>
using namespace std;
 
// Function to remove duplicate elements
// This function returns new size of modified
// array.
int removeDuplicates(int arr[], int n)
{
    if (n==0 || n==1)
        return n;
 
    // To store index of next unique element
    int j = 0;
 
    // Doing same as done in Method 1
    // Just maintaining another updated index i.e. j
    for (int i=0; i < n-1; i++)
        if (arr[i] != arr[i+1])
            arr[j++] = arr[i];
 
    arr[j++] = arr[n-1];
 
    return j;
}
 
// Driver code
int main()
{
    int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // removeDuplicates() returns new size of
    // array.
    n = removeDuplicates(arr, n);
 
    // Print updated array
    for (int i=0; i<n; i++)
        cout << arr[i] << " ";
 
    return 0;
}

Java




// simple java program to remove
// duplicates
 
class Main
{
    // Function to remove duplicate elements
    // This function returns new size of modified
    // array.
    static int removeDuplicates(int arr[], int n)
    {
        if (n == 0 || n == 1)
            return n;
      
        // To store index of next unique element
        int j = 0;
      
        // Doing same as done in Method 1
        // Just maintaining another updated index i.e. j
        for (int i = 0; i < n-1; i++)
            if (arr[i] != arr[i+1])
                arr[j++] = arr[i];
      
        arr[j++] = arr[n-1];
      
        return j;
    }
     
    public static void main (String[] args)
    {
        int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
        int n = arr.length;
         
        n = removeDuplicates(arr, n);
  
        // Print updated array
        for (int i=0; i<n; i++)
           System.out.print(arr[i]+" ");
    }
}
 
/* This code is contributed by Harsh Agarwal */

Python3 


# Python3 program to remove
# duplicate elements

# This function returns new 
# size of modified array
def removeDuplicates(arr, n):
    if n == 0 or n == 1:
        return n

    # To store index of next
    # unique element
    j = 0

    # Doing same as done
    # in Method 1 Just
    # maintaining another 
    # updated index i.e. j
    for i in range(0, n-1):
        if arr[i] != arr[i+1]:
            arr[j] = arr[i]
            j += 1

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

# Driver code
arr = [1, 2, 2, 3, 4, 4, 4, 5, 5]
n = len(arr)

# removeDuplicates() returns
# new size of array.
n = removeDuplicates(arr, n)

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


C#




// simple C# program to remove
// duplicates
using System;
 
class GfG {
     
    // Function to remove duplicate
    // elements This function returns
    // new size of modified array.
    static int removeDuplicates(int []arr, int n)
    {
         
        if (n == 0 || n == 1)
            return n;
     
        // To store index of next
        // unique element
        int j = 0;
     
        // Doing same as done in Method 1
        // Just maintaining another updated
        // index i.e. j
        for (int i = 0; i < n - 1; i++)
            if (arr[i] != arr[i + 1])
                arr[j++] = arr[i];
     
        arr[j++] = arr[n - 1];
     
        return j;
    }
     
    public static void Main ()
    {
        int []arr = {1, 2, 2, 3, 4, 4,
                                 4, 5, 5};
        int n = arr.Length;
         
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (int i = 0; i < n; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by parashar.

Javascript




<script>
// simple javascript program to remove
// duplicates
 
    // Function to remove duplicate elements
    // This function returns new size of modified
    // array.
    function removeDuplicates(arr , n) {
        if (n == 0 || n == 1)
            return n;
 
        // To store index of next unique element
        var j = 0;
 
        // Doing same as done in Method 1
        // Just maintaining another updated index i.e. j
        for (i = 0; i < n - 1; i++)
            if (arr[i] != arr[i + 1])
                arr[j++] = arr[i];
 
        arr[j++] = arr[n - 1];
 
        return j;
    }
 
     
        var arr = [ 1, 2, 2, 3, 4, 4, 4, 5, 5 ];
        var n = arr.length;
 
        n = removeDuplicates(arr, n);
 
        // Print updated array
        for (i = 0; i < n; i++)
            document.write(arr[i] + " ");
 
// This code is contributed by umadevi9616
</script>

Output

1 2 3 4 5 

Time Complexity : O(n) 
Auxiliary Space : O(1)

This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!