C++ list unique() Function
Example
Remove duplicate numbers from a sorted list:
list<int> numbers = {1, 1, 2, 3, 3, 3, 4};
numbers.unique();
for (int number : numbers) {
cout << number << "\n";
}
Definition and Usage
The unique() function removes each element that is equal to the element right before it. Only one element is left from each group of equal elements that are next to each other.
Duplicates that are not next to each other are not removed. Use sort() first to put equal elements next to each other.
The unique function is defined in the <list> header file.
Syntax
list.unique();
Parameter Values
None.
Technical Details
| Returns: | Nothing (void). |
|---|
Related Pages
Read more about lists in our List Tutorial.