C++ How To Remove Duplicates from an Array
Remove Duplicates from an Array
A set can only store each value once. Add all elements to a set, and the duplicates are removed automatically:
Example
int numbers[] = {1, 2, 2, 3, 4, 4, 5};
set<int> unique;
for (int num : numbers) {
unique.insert(num);
}
for (int num : unique) {
cout << num << " ";
}
Explanation:
When insert() gets a value that is already in the set, nothing happens.
Note: A set also sorts its values.