Removing Duplicates from a Range

What is the most efficient way to remove duplicates from a range using standard algorithms?

To remove duplicates from a range using standard algorithms, you can use a combination of std::ranges::sort() and std::ranges::unique().

This approach ensures that the range is first sorted, which is a prerequisite for std::ranges::unique() to work correctly.

Here is an example demonstrating this process:

#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>

int main() {
  std::vector<int> numbers{5, 3, 8, 3, 2, 8, 1, 5};

  // Sort the range first
  std::ranges::sort(numbers);

  // Remove duplicates
  auto last = std::ranges::unique(numbers);
  numbers.erase(last.begin(), numbers.end());  

  for (int n : numbers) {
    std::cout << n << ", ";
  }
}
1, 2, 3, 5, 8,

In this example:

  1. std::ranges::sort(numbers) sorts the elements in the numbers vector.
  2. std::ranges::unique(numbers) rearranges the elements such that each unique element appears only once, and returns a subrange of the unique elements.
  3. numbers.erase(last.begin(), numbers.end()) removes the duplicate elements from the vector.

This approach is efficient and leverages the power of the C++ standard library's range algorithms.

Alternatively, you can use a std::set to remove duplicates, which automatically handles uniqueness but does not preserve the original order:

#include <iostream>
#include <set>
#include <vector>

int main() {
  std::vector<int> numbers{5, 3, 8, 3, 2, 8, 1, 5};

  // Use a set to remove duplicates
  std::set<int> unique_numbers(
    numbers.begin(), numbers.end());

  for (int n : unique_numbers) {
    std::cout << n << ", ";
  }
}
1, 2, 3, 5, 8,

Using a std::set is straightforward but does not maintain the original order of elements. If preserving order is important, you should use the std::ranges::sort() and std::ranges::unique() approach.

For better performance, especially with large datasets, the sorting and unique algorithms are generally preferred as they are optimized for such operations in the standard library.

8 Key Standard Library Algorithms

An introduction to 8 more useful algorithms from the standard library, and how we can use them alongside views, projections, and other techniques

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Using std::ranges::for_each() with a Member Function
How do I use std::ranges::for_each() with a member function of a class?
Comparing Only Part of Two Ranges
Can I use std::ranges::equal() to compare only a part of two ranges?
Using std::iota() with a Custom Step Size
How can I use std::ranges::iota() to generate a sequence with a custom step size?
Merging Three or More Sorted Ranges
How do I merge three or more sorted ranges using std::ranges::merge()?
Ensuring No Repeated Elements in std::ranges::sample()
Can I use std::ranges::sample() to ensure no repeated elements in the sample?
Using Subranges vs Iterator Pairs
What are the use cases for using std::ranges::subrange() versus standard iterator pairs?
std::ranges::set_union() vs std::ranges::merge()
What are the benefits of using std::ranges::set_union() over std::ranges::merge() in terms of performance and use cases?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant