C++ Program To Remove Duplicates From Sorted Array

Last Updated : 20 Aug, 2026

Given a sorted array, the task is to remove duplicate elements while preserving the order of the unique elements.

  • The input array is sorted, so duplicate elements occur consecutively.
  • The function returns the new size of the array after removing duplicates.

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

Approaches to Remove Duplicates

Since the array is sorted, duplicates can be identified by comparing adjacent elements. This can be done using an auxiliary array or directly within the original array.

Removing Duplicates Using Extra Space

The first approach uses an auxiliary array to store the unique elements.

Steps:

  1. Create a temporary array temp[].
  2. Traverse the sorted array and copy each unique element into temp[].
  3. Copy the unique elements back into the original array.
  4. Return the number of unique elements.
C++
#include <iostream>
using namespace std;

int removeDuplicates(int arr[], int n)
{
    if (n <= 1)
        return n;

    int temp[n];
    int j = 0;

    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i] != arr[i + 1])
            temp[j++] = arr[i];
    }

    // Store the last element
    temp[j++] = arr[n - 1];

    // Copy unique elements back to the original array
    for (int i = 0; i < j; i++)
        arr[i] = temp[i];

    return j;
}

int main()
{
    int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    n = removeDuplicates(arr, n);

    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
} 

Output
1 2 3 4 5 

Explanation: Because the array is sorted, duplicate elements are adjacent. The algorithm compares each element with the next one and stores the element only when the two are different. The last element is added separately because it has no next element for comparison.

Removing Duplicates Using Constant Extra Space

The array can be modified in place by maintaining a separate index j that tracks the position where the next unique element should be stored.

Steps:

  1. Initialize j = 0.
  2. Traverse the array and compare each element with the next element.
  3. When two adjacent elements are different, store the current element at arr[j] and increment j.
  4. Store the last element at the next available position.
  5. Return j as the new size.
C++
#include <iostream>
using namespace std;

int removeDuplicates(int arr[], int n)
{
    if (n <= 1)
        return n;

    int j = 0;

    for (int i = 0; i < n - 1; i++)
    {
        if (arr[i] != arr[i + 1])
            arr[j++] = arr[i];
    }

    // Store the last unique element
    arr[j++] = arr[n - 1];

    return j;
}

int main()
{
    int arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    n = removeDuplicates(arr, n);

    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";

    return 0;
} 
Try It Yourself
redirect icon

Output
1 2 3 4 5 

Explanation:

  • The variable j acts as the index for the next unique element. Whenever arr[i] differs from arr[i + 1], it is placed at arr[j]. Since the elements are written back into the same array, no auxiliary array is required.
  • Only the first j positions contain the resulting unique elements; the remaining positions can contain leftover values and are not considered part of the updated array.
Comment