Remove Duplicates From Sorted Array In C++

We will be given a sorted array as input. Our task is to remove the duplicate elements from the collection and return the function to the primary room, where we can start printing down the new sorted array without duplicates.

We can solve the problem in two ways using extra space and the other is using constant extra space, which is discussed below with code and outputs.

C++ Code: Using Extra Space

Example

// Here in the below notes, we are writing down the C++ programming language 
// code to demonstrate the concept of a Simple C++ program to
// Remove duplicates from the sorted array with code and example, and their 
// respective output.
#include <iostream>
using namespace std;

// the below function which we have written will help us with removing the 
// duplicate elements in the array which are passed into it as an argument
// it will return us the size of the new modified array once finishing 
// the operation of removing the duplicate elements
int removeDuplicates(int arr[], int n)
{
	
	
	// this is a small (if) check condition where we will be checking if 
	// whether the element which is passed as an argument contains one element
	// or no elements and return the n value if the condition is true.
	if (n == 0 || n == 1)
		return n;

	int temp[n];

	// from j equals to 0 onwards; we start traversing the elements in the
	// array passed as an argument
	
	// we have an uninitialised variable j to 0, which can then start 
	// traversing from 0 to the ray's size w, n-1.
	
	int j = 0;
	
	// this simple for loop has a  simple check condition where we check if 
	// the present element where we are trying to store is equal to the next 
	// element or not
	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 been used previously
	// whether the last part of the array is individual, a repeated one
   // we are trying to keep here what was not, therefore in the code
	temp[j++] = arr[n - 1];

	// we are trying to store the array in the temporary array created 
	// after modifying the original array using a simple for loop
	for (int i = 0; i < j; i++)
		arr[i] = temp[i];

	return j;
}

// the main driver code functionality starts from here
int main()
{
	int arr[] = { 21, 22, 22, 23, 24, 24, 24, 25, 25 };
	int n = sizeof(arr) / sizeof(arr[0]);

	// the C++ programming language has in-built removeDuplicates()
	// function, which helps us with not writing huge lines of code instead 
	// do the task in lightning less time complexity.
   // after operating, it returns to us the size of the new array
	n = removeDuplicates(arr, n);

	
	// the below small for-loop code snippet helps us with printing down the
	// updated array after removing the duplicate elements to the display
	for (int i = 0; i < n; i++)
		cout << arr[i] << " ";

	return 0;
}

Output:

Output

31 32 33 34 35

1. Constant Extra Space

Example

C++ code
// Here in the below notes, we are writing down the C++ programming language
// code to demonstrate the concept of a Simple C++ program to
// Remove duplicates from the sorted array with code and example, and their 
// respective output.
#include<iostream>
using namespace std;

// the remove duplicates in-built function helps us not to write 
// the same extra lines of code
int removeDuplicates(int arr[], int n)
{
	if (n==0 || n==1)
		return n;

	// from j equals to 0 onwards; we start traversing the elements in the
	// array passed as an argument

	int j = 0;

	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;
}

// the main driver code functionality starts from here
int main()
{
	int arr[] = {31, 32, 32, 33, 34, 34, 34, 35, 35};
	int n = sizeof(arr) / sizeof(arr[0]);

	
	n = removeDuplicates(arr, n);

	// the below small for-loop code snippet helps us with printing down the
	// updated array after removing the duplicate elements from the display

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

	return 0;
}

Output:

Output

31 32 33 34 35

Input Required

This code uses input(). Please provide values below: