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:

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

*chevron_right*

*filter_none*

**Output:**

Before removing duplicates: 4 2 3 3 2 4 After removing duplicates: 2, 3, 4,

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Check if a given array contains duplicate elements within k distance from each other
- Remove minimum elements from the array such that 2*min becomes more than max
- Remove elements from the array which appear more than k times
- Remove minimum elements from array so that max <= 2 * min
- Remove elements to make array sorted
- Remove minimum elements from ends of array so that sum decreases by at least K | O(N)
- Remove elements from the array whose frequency lies in the range [l, r]
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Remove minimum number of elements such that no common element exist in both array
- Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
- Last duplicate element in a sorted array
- Insert duplicate of K adjacent to it for it's every occurrence in array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Check if Array elements can be maximized upto M by adding all elements from another array
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove elements that appear strictly less than k times
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Remove k corner elements to maximize remaining sum
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Count of Array elements greater than all elements on its left and at least K elements on its right

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.