Remove duplicates from an unsorted array using STL in C++
Last Updated :
27 Sep, 2023
Given an unsorted array, the task is to remove the duplicate elements from the array using STL in C++
Examples:
Input: arr[] = {1, 2, 5, 1, 7, 2, 4, 2}
Output: arr[] = {1, 2, 4, 5, 7}
Input: arr[] = {1, 2, 4, 3, 5, 4, 4, 2, 5}
Output: arr[] = {1, 2, 3, 4, 5}
Approach:
The duplicates of the array can be removed using the unique() function provided in STL.
Below is the implementation of the above approach.
CPP
#include <bits/stdc++.h>
using namespace std;
// Function to remove duplicate elements
void remDup(int arr[], int n)
{
// Initialise a vector
// to store the array values
// and an iterator
// to traverse this vector
vector<int> v(arr, arr + n);
vector<int>::iterator it;
// sorting vector
sort(v.begin(), v.end());
// using unique() method
// to remove duplicates
it = unique(v.begin(), v.end());
// resize the new vector
v.resize(distance(v.begin(), it));
// Print the array with duplicates removed
cout << "\nAfter removing duplicates:\n";
for (it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
cout << '\n';
}
// Driver code
int main()
{
int arr[] = { 1, 2, 5, 1, 7, 2, 4, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
// Print array
cout << "\nBefore removing duplicates:\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
// call remDup()
remDup(arr, n);
return 0;
}
[tabbyending]
OutputBefore removing duplicates:
1 2 5 1 7 2 4 2
After removing duplicates:
1 2 4 5 7
Complexity:
Time complexity: O(nlog(n)) for sorting.
Space complexity: O(n) for storing the array into the vector.
Similar Reads
Remove duplicates from a sorted array using STL in C++ Given a sorted array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: The duplicates of the array can be removed using the un
2 min read
Remove duplicates from a string using STL in C++ Given a string S, remove duplicates in this string using STL in C++ Examples: Input: Geeks for geeks Output: Gefgkors Input: aaaaabbbbbb Output: ab Approach: The consecutive duplicates of the string can be removed using the unique() function provided in STL. Below is the implementation of the above
1 min read
Remove duplicate elements in an Array using STL in C++ Given an array, the task is to remove the duplicate elements from the array using STL in C++ Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 5} Approach: This can be done using set in standard template library. Set
2 min read
How to sort an Array using STL in C++? Given an array arr[], sort this array using STL in C++. Example: Input: arr[] = {1, 45, 54, 71, 76, 12} Output: {1, 12, 45, 54, 71, 76} Input: arr[] = {1, 7, 5, 4, 6, 12} Output: {1, 4, 5, 6, 7, 12} Approach: Sorting can be done with the help of sort() function provided in STL. Syntax: sort(arr, arr
1 min read
Different Ways to Remove an Element From Set in C++ STL Prerequisite: Set in C++ There are multiple ways to remove an element from the set. These are as follows: Removing an element by its valueRemoving an element by its indexRemoving an element by an iterator Example: Input set s={10, 20, 30, 40, 50} , value=40 // Removing 40 from the set Output set s={
3 min read