Remove duplicate elements in an Array using STL in C++ Last Updated : 31 May, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 type variable in STL automatically removes duplicating element when we store the element in it. Below is the implementation of the above approach: CPP // C++ program to remove the // duplicate elements from the array // using STL in C++ #include <bits/stdc++.h> using namespace std; // Function to remove duplicate elements void removeDuplicates(int arr[], int n) { int i; // Initialise a set // to store the array values set<int> s; // Insert the array elements // into the set for (i = 0; i < n; i++) { // insert into set s.insert(arr[i]); } set<int>::iterator it; // Print the array with duplicates removed cout << "\nAfter removing duplicates:\n"; for (it = s.begin(); it != s.end(); ++it) cout << *it << ", "; cout << '\n'; } // Driver code int main() { int arr[] = { 4, 2, 3, 3, 2, 4 }; 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 removeDuplicates() removeDuplicates(arr, n); return 0; } Output:Before removing duplicates: 4 2 3 3 2 4 After removing duplicates: 2, 3, 4, Time Complexity : O(NlogN) Space Complexity : O(N) Comment More infoAdvertise with us Next Article C++ Remove Elements From a List While Iterating S shivamkukreti Follow Improve Article Tags : C++ STL cpp-array Practice Tags : CPPSTL 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 an unsorted array using STL in C++ 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 2 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 Delete elements in C++ STL list How to insert elements in C++ STL List ? This article covers the deletion aspects in STL list. Using list::erase(): The purpose of this function is to remove the elements from list. Single or multiple contiguous elements in range can be removed using this function. This function takes 2 arguments, s 4 min read C++ Remove Elements From a List While Iterating Prerequisite: List in C++ Removing elements is a necessary task for any container. Removing elements from a list is an easy task as we have predefined pop_back and pop_front functions but as the list is a container following the properties of a doubly linked list there should be a method to remove e 5 min read list remove() function in C++ STL The list::remove() is a built-in function in C++ STL which is used to remove elements from a list container. It removes elements comparing to a value. It takes a value as the parameter and removes all the elements from the list container whose value is equal to the value passed in the parameter of t 2 min read Like