×

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

Palindrome using Do-while loop in C++

What is Palindrome? A palindrome is a word, number, phrase, or other sequence of letters that reads the same backward as forward, such as 101 or MOM. Like other programming languages, C++...

5 minutes read.

Default arguments in C++

Arguments in a function are defined as the values supplied when the function is called. The source is the values supplied, and the destination is the receiving function. Let us...

3 minutes read.

Free vs delete() in C++

Free vs delete() in C++ In this section, we will learn about the free() function and also create a C ++ program of the delete operator. What is free() Function in C++? In...

4 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.

New Operator in C++

Dynamic memory allocation in C++ means manually allocating the memory by the developer duing run-time. The dynamic memory is allocated in the heap section of the RAM, whereas the static...

3 minutes read.

C++ Aggregation

C++ Aggregation Definition: In C++, aggregation is a process in which one class (as an entity reference) defines another class. It provides another way to reuse the class. It represents...

4 minutes read.

Name Mangling and extern in C++

Name Mangling and Function Overloading: Function overloading is a feature offered by C++. As long as each function accepts various parameters, we can use this to write many functions with the...

4 minutes read.

RTTI (Run-Time Type Information) in C++

In C++, RTTI or Run-Time Type Information reveals information about the data type of an object at runtime and only works with classes that have at least one virtual function....

3 minutes read.

SET Data Structure in C++

Generally, in our home, we store food items in a fridge or kitchen efficiently to find them easily and use them. Similarly, in programming, we need to store the data...

6 minutes read.

C++ References

C++ References An alias is a reference variable that is another name for a variable already in existence. If a relation is initialized with a variable, it is possible to use...

3 minutes read.

Array of Vectors in C++ STL

Prerequisites: C++ STL Arrays and C++ STL Vector. A group of items kept in consecutive memory region is known as an array. It is to group similar objects of the same...

4 minutes read.

C++ this pointer

'this' is a pointer that points to the object for which this function was called. The 'this' pointer holds the memory address of the current object. The 'this' pointer is implicitly passed to...

2 minutes read.

How to build a program in C++

Building a program is all about creating the program and executing it successfully. There are some steps  precisely, which must be followed to make the program. Step 1: Get an IDE...

4 minutes read.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

Structure of C++ Program

Many people believe that C++, an object-oriented programming (OOP) language, is the finest language for developing demanding applications. A superset of the C language is C++. Java, a closely comparable...

4 minutes read.

How to initialize a dynamic array in C++

Regular arrays or static arrays have a predetermined size or fixed size. Change in the size of regular arrays is not possible. The memory size for static arrays determines at compile...

4 minutes read.

C++ 11 vs C++ 14 vs C++ 17

C++ is a language that is used to create a high-performance application. C++ 11, C++ 14, and C++ 17 are the different version of C++. There are some differences between...

1 minute read.

C++ Virtual Function

A virtual function is such function which is declared inside the base class and redefined by the derive class. C++ uses a virtual keyword to make a function as a virtual function. The virtual...

1 minute read.

Two dimension array

C++ Two dimension (2D) Array Two dimension (2D) array is an array of arrays. It is represented in the form of row and column. The elements of 2D array are accessed through the...

2 minutes read.

C++ Program: Matrix Multiplication

Matrix Multiplication in C++ What is a Matrix? A matrix is a set of numbers in the form of rows and columns forming a rectangular array. It includes numbers, which are often...

4 minutes read.