Which Queue ensures uniqueness of the elements?
Last Updated :
26 Oct, 2023
A queue is a linear data structure that follows the principle of First In First Out (FIFO). The first element added to the queue will be the first element to be removed from the queue. The queue is a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end. The element which is first pushed into the order, the delete operation is first performed on that.
Which Queue ensures the uniqueness of the elements?
A data structure called a SetQueue includes the attributes of both sets and queues. It keeps a queue of the same components in the order they were introduced to the queue as well as a collection of unique elements
Features of SetQueue:
- When we need to keep track of a collection of distinct items and process them in the order they were added, a SetQueue comes in handy.
- In web crawling or web scraping, when we must keep track of a list of specific URLs to visit and visit those URLs in the order in which they were found, SetQueues is frequently used.
- Using a SetQueue over a standard queue or set has the main benefit of ensuring element uniqueness while also maintaining the sequence of insertion. As a result, processing pieces in the order they were added while eliminating duplications is made simple.
- Depending on the underlying data structures used to maintain the set and the queue, different SetQueue implementations may be utilized. A set is used to retain the unique elements in the sample implementation and a deque is used to keep the order of insertion. However, other data structures can also be employed, such as linked lists or hash tables.
- In general, SetQueues is a helpful data structure for keeping track of a collection of distinct elements while simultaneously maintaining the order of insertion. They are frequently employed in web crawling, web scraping, and other processes where distinct items must be handled in a particular order.
The SetQueue class has three methods:
- By constructing two data structures: a set to keep track of the unique components and a deque to keep track of the order of insertion— the __init__ method initializes the SetQueue.
Python3
def __init__( self ):
self . set = set ()
self .queue = deque()
|
- The SetQueue receives an element through the enqueue method. This method first checks if the value is already in the set. If it is not in the set, it adds the value to the set and also appends the value to the deque.
Python3
def enqueue( self , value):
if value not in self . set :
self . set .add(value)
self .queue.append(value)
|
- The dequeue method removes the first element from the SetQueue.This method first checks if the deque is not empty. If it is not empty, it removes and returns the first element from the deque. It also removes the same element from the set.
Python3
def dequeue( self ):
if len ( self .queue) > 0 :
value = self .queue.popleft()
self . set .remove(value)
return value
|
Below is the clear implementation of the above functions:
C++
#include <iostream>
#include <unordered_set>
#include <queue>
template < typename T>
class SetQueue {
private :
std::unordered_set<T> set;
std::queue<T> queue;
public :
SetQueue() {
}
void enqueue(T value) {
if (set.find(value) == set.end()) {
set.insert(value);
queue.push(value);
}
}
T dequeue() {
if (!queue.empty()) {
T value = queue.front();
queue.pop();
set.erase(value);
return value;
}
return T();
}
int size() const {
return queue.size();
}
};
int main() {
SetQueue< int > sq;
sq.enqueue(1);
sq.enqueue(2);
sq.enqueue(3);
sq.enqueue(2);
std::cout << sq.dequeue() << std::endl;
std::cout << sq.dequeue() << std::endl;
sq.enqueue(4);
sq.enqueue(3);
std::cout << sq.dequeue() << std::endl;
std::cout << sq.dequeue() << std::endl;
std::cout << sq.size() << std::endl;
return 0;
}
|
Java
import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
class SetQueue<T> {
private Set<T> set;
private Queue<T> queue;
public SetQueue() {
this .set = new HashSet<>();
this .queue = new ArrayDeque<>();
}
public void enqueue(T value) {
if (!set.contains(value)) {
set.add(value);
queue.offer(value);
}
}
public T dequeue() {
T value = queue.poll();
if (value != null ) {
set.remove(value);
}
return value;
}
public int size() {
return queue.size();
}
}
public class Main {
public static void main(String[] args) {
SetQueue<Integer> sq = new SetQueue<>();
sq.enqueue( 1 );
sq.enqueue( 2 );
sq.enqueue( 3 );
sq.enqueue( 2 );
System.out.println(sq.dequeue());
System.out.println(sq.dequeue());
sq.enqueue( 4 );
sq.enqueue( 3 );
System.out.println(sq.dequeue());
System.out.println(sq.dequeue());
System.out.println(sq.size());
}
}
|
Python3
from collections import deque
class SetQueue:
def __init__( self ):
self . set = set ()
self .queue = deque()
def enqueue( self , value):
if value not in self . set :
self . set .add(value)
self .queue.append(value)
def dequeue( self ):
if len ( self .queue) > 0 :
value = self .queue.popleft()
self . set .remove(value)
return value
def __len__( self ):
return len ( self .queue)
sq = SetQueue()
sq.enqueue( 1 )
sq.enqueue( 2 )
sq.enqueue( 3 )
sq.enqueue( 2 )
print (sq.dequeue())
print (sq.dequeue())
sq.enqueue( 4 )
sq.enqueue( 3 )
print (sq.dequeue())
print (sq.dequeue())
print ( len (sq))
|
C#
using System;
using System.Collections.Generic;
class SetQueue<T>
{
private HashSet<T> set = new HashSet<T>();
private Queue<T> queue = new Queue<T>();
public SetQueue()
{
}
public void Enqueue(T value)
{
if (! set .Contains(value))
{
set .Add(value);
queue.Enqueue(value);
}
}
public T Dequeue()
{
if (queue.Count > 0)
{
T value = queue.Dequeue();
set .Remove(value);
return value;
}
return default (T);
}
public int Size()
{
return queue.Count;
}
}
class Program
{
static void Main()
{
SetQueue< int > sq = new SetQueue< int >();
sq.Enqueue(1);
sq.Enqueue(2);
sq.Enqueue(3);
sq.Enqueue(2);
Console.WriteLine(sq.Dequeue());
Console.WriteLine(sq.Dequeue());
sq.Enqueue(4);
sq.Enqueue(3);
Console.WriteLine(sq.Dequeue());
Console.WriteLine(sq.Dequeue());
Console.WriteLine(sq.Size());
}
}
|
Javascript
class GFG {
constructor() {
this .set = new Set();
this .queue = [];
}
enqueue(value) {
if (! this .set.has(value)) {
this .set.add(value);
this .queue.push(value);
}
}
dequeue() {
if ( this .queue.length > 0) {
const value = this .queue.shift();
this .set. delete (value);
return value;
}
return null ;
}
size() {
return this .queue.length;
}
}
function main() {
const sq = new GFG();
sq.enqueue(1);
sq.enqueue(2);
sq.enqueue(3);
sq.enqueue(2);
console.log(sq.dequeue());
console.log(sq.dequeue());
sq.enqueue(4);
sq.enqueue(3);
console.log(sq.dequeue());
console.log(sq.dequeue());
console.log(sq.size());
}
main();
|
Time Complexity: O(1)
Auxiliary Space: O(n)
Advantages of SetQueue:
- It ensures that no duplicates are added.
- Enqueuing and dequeuing operations both have a time complexity of O(1) on average, which is very efficient.
- SetQueue can be used in situations where maintaining a set of unique elements in the order they were added is important.
Disadvantages of SetQueue:
- It requires more memory than using just one data structure.
- It does not preserve the order of elements that were enqueued if they were dequeued in a different order.
- SetQueue can be less efficient than a regular deque if the elements being added are always unique.
Share your thoughts in the comments
Please Login to comment...