Daily bit(e) of C++ | std::unique, std::unique_copy
Daily bit(e) of C++ ♻️25, The two algorithms that remove (std::unique) and filter (std::unique_copy) duplicate values.
The std::unique algorithm is typically used on a sorted range to produce the list of unique values.
However, the algorithm is more straightforward than that. It can operate on any forward range and will remove consecutive duplicate values (by shifting the elements in the range to the left). Applying std::unique to a sorted range results in a range of unique values because all duplicates are consecutive.
The copy variant std::unique_copy will emit the unique values through the provided output iterator instead.
Both algorithms provide a C++17 parallel, and C++20 ranges variant.