﻿ C++ Remove all duplicate elements from a queue

# C++ Queue Exercises: Remove all duplicate elements from a queue

## C++ Queue: Exercise-17 with Solution

Write a C++ program to remove all duplicate elements from a queue.

Sample Solution:

C Code:

`````` #include <iostream>

using namespace std;

const int MAX_SIZE = 100;

class Queue {
private:
int front; // Index of the front element in the queue
int rear; // Index of the rear element in the queue
int arr[MAX_SIZE]; // Array to hold queue elements

public:
Queue() {
front = -1; // Initializing front index to -1
rear = -1; // Initializing rear index to -1
}

bool isFull() {
return (rear == MAX_SIZE - 1); // Checks if the queue is full
}

bool isEmpty() {
return (front == -1 && rear == -1); // Checks if the queue is empty
}

void enqueue(int x) {
if (isFull()) {
cout << "Error: Queue is full" << endl; // Display error if the queue is full
return;
}
if (isEmpty()) {
front = 0;
rear = 0;
}
else {
rear++;
}
arr[rear] = x; // Adds an element to the rear of the queue
}

void dequeue() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error if the queue is empty
return;
}
if (front == rear) {
front = -1;
rear = -1;
}
else {
front++;
}
}

int peek() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error if the queue is empty
return -1;
}
return arr[front]; // Returns the element at the front of the queue
}

void display() {
if (isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error if the queue is empty
return;
}
cout << "Queue elements are: ";
for (int i = front; i <= rear; i++) {
cout << arr[i] << " "; // Displays all elements in the queue
}
cout << endl;
}

void remove_duplicates(Queue &q) {
if (q.isEmpty()) {
cout << "Error: Queue is empty" << endl; // Display error if the queue is empty
return;
}
int n = q.rear - q.front + 1; // Calculate the number of elements in the queue
int uniqueElements[n]; // Array to store unique elements
int idx = 0;
for (int i = q.front; i <= q.rear; i++) {
bool isDuplicate = false;
for (int j = q.front; j < i; j++) {
if (q.arr[i] == q.arr[j]) {
isDuplicate = true; // Check if the element is a duplicate
break;
}
}
if (!isDuplicate) {
uniqueElements[idx] = q.arr[i]; // Store unique elements
idx++;
}
}
q.front = 0;
q.rear = idx - 1;
for (int i = 0; i < idx; i++) {
q.arr[i] = uniqueElements[i]; // Update the queue with unique elements
}
}
};

int main() {
cout << "Initialize a Queue." << endl;
Queue q;
cout << "\nInsert some elements into the queue:" << endl;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
q.enqueue(5);
q.enqueue(5);
q.enqueue(6);
q.enqueue(1);
q.display();
cout << "\nRemove all duplicates from the said queue:" << endl;
q.remove_duplicates(q);
q.display();
return 0;
}
```
```

Sample Output:

```Initialize a Queue.

Insert some elements into the queue:
Queue elements are: 1 2 3 5 5 6 1

Remove all duplicates from the said queue:
Queue elements are: 1 2 3 5 6
```

Flowchart:

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Remove all odd elements from a queue.
Next C++ Exercise: Remove all elements greater than a number from a queue.

What is the difficulty level of this exercise?

﻿