Priority Queue with Unique Elements

How can I ensure that a priority queue contains only unique elements?

To ensure that a priority queue contains only unique elements, you can use a combination of a priority queue and a set data structure. The set will keep track of the unique elements, while the priority queue will maintain the heap property based on the priority of the elements.

Here's an example of how you can implement a priority queue with unique elements:

#include <iostream>
#include <queue>
#include <unordered_set>

int main() {
  std::priority_queue<int> pq;
  std::unordered_set<int> uniqueElements;

  auto insertElement = [&](int element) {
    if (uniqueElements.insert(element).second) {
      pq.push(element);
    }
  };

  insertElement(10);
  insertElement(30);
  insertElement(20);
  insertElement(30); // Duplicate not inserted
  insertElement(40);

  while (!pq.empty()) {
    std::cout << pq.top() << " ";
    pq.pop();
  }
}

In this example, we create a std::priority_queue<int> to store the elements and a std::unordered_set<int> to keep track of the unique elements.

We define a lambda function insertElement that takes an element as input. Inside the lambda function, we attempt to insert the element into the uniqueElements set using the insert() method. The insert() method returns a pair, where the second element is a boolean indicating whether the insertion was successful (i.e., the element was not already present in the set).

If the insertion into the set is successful, we push the element into the priority queue using pq.push(). This ensures that only unique elements are added to the priority queue.

When you run this code, it will output:

40 30 20 10

As you can see, the duplicate element 30 is not inserted into the priority queue.

By using a set to keep track of the unique elements, we can efficiently check if an element is already present before inserting it into the priority queue. The time complexity of insertion into both the set and the priority queue is O(log n), where n is the number of elements.

Note that using a set to maintain uniqueness adds some overhead compared to a regular priority queue, so use this approach only if maintaining uniqueness is a requirement for your specific use case.

Priority Queues using std::priority_queue

Learn how to access objects based on their importance, and how to customise how that priority is determined

Questions & Answers

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

Custom Comparison Function for Priority Queue
How can I define a custom comparison function for a priority queue of custom objects?
Removing a Specific Element from Priority Queue
Is it possible to remove a specific element from a priority queue without popping the top element?
Priority Queue with Dynamic Priorities
Can I update the priority of an element already in the priority queue?
Priority Queue with Stable Ordering
How can I ensure stable ordering in a priority queue when elements have the same priority?
Priority Queue with Custom Container
Can I use a custom container as the underlying container for a priority queue?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant