×

Remove duplicates from sorted array in C++

Remove duplicates from the sorted array in C++.

This same process, due to a sorted array, is to erase the redundant components 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)

  • Generate an auxiliary temp[] array for storing different aspects.
  • Traverse the input array and duplicate different aspects from arr[] to temp[] one by one. Keep records of counting different aspects, as well. Let the count be j.
  • Copy j elements to arr[] from temp, and return j.

Example:

// Simple C++ program to remove duplicates
#include<iostream>
using namespace std;
/* Removes duplicate components
The above function will return the prototype model size
Display.*/
int removeDuplicates(int array[], int p)
{
          // Return, if array is empty
          // or contains a single element
          if (p==0 || p==1)
                   return p;
          int temp[p];
          // Start traversing elements
          int k = 0;
          for (int i=0; i<p-1; i++)
                   // If current element is not equal
                   // to next element then store that
                   // current element
                   if (array[i] != array[i+1])
                             temp[k++] = array[i];
          // Store the last element as whether
          // it is unique or repeated, it hasn't
          // stored previously
          temp[k++] = array[p-1];
          // Modify original array
          for (int i=0; i<k; i++)
                   array[i] = temp[i];
          return k;
}
// Driver code
int main()
{
          int array[] = {1, 2, 2, 3, 4, 4, 4, 5, 5, 6, 7, 7, 8, 8, 8, 9, 9 ,10, 11};
          int p = sizeof(array) / sizeof(array[0]);
          // removeDuplicates() returns new size of
          // array.
          p = removeDuplicates(array, p);
          // Print updated array
          for (int i=0; i<p; i++)
          cout << array[i] << " ";
          return 0;
}

Output:

Remove duplicates from sorted array in CPP

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

Method 2: (Constant extra space)

Just hold a totally separate index for the same array as kept in Method 1 for the different array.

Example:

/* C++ program to remove duplicates in-place*/
#include<iostream>
using namespace std;
int removeDuplicates(int array[], int t)
{
          if (t==0 || t==1)
                   return t;
          // To store index of next unique element
          int k = 0;
          for (int i=0; i < t-1; i++)
                   if (array[i] != array[i+1])
                             array[k++] = array[i];
          array[k++] = array[t-1];
          return k;
}
// Driver code
int main()
{
          int array[] = {1, 1, 1, 2, 2, 2, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9};
          int t = sizeof(array) / sizeof(array[0]);
          // removeDuplicates() returns new size of
          // array.
          t = removeDuplicates(array, t);
          for (int i=0; i<t; i++)
                   cout << array[i] << " ";
          return 0;
}

Output:

Remove duplicates from sorted array in CPP

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


Related Topics

C++ String Class and its Applications

The String class is available in C++. The character array is represented by the C string. The string class in C++ has a few different attributes. It contains several functions...

4 minutes read.

Include Guards in C++

In C++ programming, we frequently utilize a class more than once, so it is necessary to create a header file and include it in the main program. Now, occasionally a...

3 minutes read.

C++ String Concatenation

C++ String Concatenation In this section, we will learn about C ++ String Concatenation, what it does, how it works, and will also see its programs. What is the String Concatenation? The + operator...

3 minutes read.

C++ If

C++ Control Statement C++ control statement or decision-making statement is used to control the flow of program statement according to condition applied. C++ if Control Statement An if control statement in C++ is used to...

2 minutes read.

C++ Encapsulation

The following two essential components are present in all C++ programmes: Functions are the parts of a programme that perform actions, and they are termed programme statements (code).Program data is the...

4 minutes read.

Binary to Decimal in C++

We must create a software to convert a binary number into an equivalent decimal value given a binary number as input. Example: // C++ program to convert binary to decimal #include < iostream...

3 minutes read.

fscanf() Function in the C++

In the C++ programming language, the fscanf() method can be used to read data from a file stream. Syntax: The syntax for the fscanf() function in the C++ programming language is as...

3 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

C++ Output Iterators

Iterators : Iterators serve as a link between algorithms and STL containers, allowing the data inside the container to be modified. They let you to iterate through the container, access and...

4 minutes read.

Initialization of Data Members

In this tutorial, we'll look at how to initialise static member variables in C++. Static members, such as functions or variables, can be added to C++ classes. After declaring the...

1 minute read.

Top 14 Best Free C++ IDE (Editor & Compiler) for Windows in 2024

Bjarne Stroustrup created the all-purpose object-oriented programming language C++. To develop C++ programs, there are various Integrated Development Environments (IDE) that offer prewritten code templates. These programs automatically modify the...

6 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ History

C++ is a middle-level programming language developed in 1980s by Bjarne Stroustrup at Bel Labs. C ++ development initially started in 1979, four years before its launch. It is started with...

1 minute read.

Structure Vs Class in C++

The structure in C++ is similar to that of a class, with a few exceptions. Both the structure and the stage, the most important thing is safety. The property is...

4 minutes read.

C++ First Program

Let's write a simple basic program structure of C++, its compilation and its execution (how it runs). This program is compiled using GCC compiler. Open any editor to write C++ program. #include<iostream>   using namespace std;   int main(){       cout<<"Welcome to C++ program"<<endl;   } Output...

2 minutes read.

getline() Function and Character Array in C++

In this article, we will explore about some concepts on getline() function and character array in the most useful language C++. The getline() method in C++ is simply a standard library...

6 minutes read.

This keyword in C++

This keyword in C++ is a pointer which points to the object. It is passed as an argument to functions which helps in accessing the object. It is used for a...

3 minutes read.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

C++ If-else-if

Introduction: If-else-if control statement is an if statement used with an optional else if control statement to check multiple conditions. In this control statement, when any one of the condition returns...

4 minutes read.