We need to implement findFrequncy(k) operation for a queue along with normal enqueue() operation. The findFrequency(k) function should return frequency of item k in queue.
Given an empty queue and two arrays, the task is to insert elements of the first array into a queue and find frequencies of the items in the second array.
Examples:
Input: insert[] = [ 1, 2, 3, 4, 5, 2, 3, 1 ] , findFreq[] = [ 1, 3, 2, 9, 10 ]
Output:
2
2
2
-1
-1
Explanation: After inserting 1, 2, 3, 4, 5, 2, 3, 1 into the queue, frequency of 1 is 2, 3 is 2, 2 is 2, 9 is -1 and 10 is -1 (since, 9 and 10 is not there in the queue).
Input: insert[] = [ 1, 2, 1, 1, 1, 4 ] , findFreq[] = [ 1, 5, 4, 3 ]
Output:
4
-1
1
-1
Explanation: After inserting 1, 2, 1, 1, 1 and 4 into the queue, frequency of 1 is 4 and that of 4 is 1. Since 5 and 3 are not there in the queue we output -1 for them
To find frequency of an item, we need to iterate through the queue and increment count whenever current queue element matches with k. To iterate through queue, we pop items and push back. If pop and push all items in a queue, we get the same queue back as queue follows FIFO (First In First Out Order order. Please refer the below illustration for details.
Illustration of findFrequency(k) :
k = 10
q = [10, 20, 30, 10]
Initialize : Frequency of k, res = 0
After pop() and push()
q = [20, 30, 10, 10], res = 1
After pop() and push()
q = [30, 10, 10, 20], res = 1
After pop() and push()
q = [10, 10, 20, 30], res = 1
After pop() and push()
q = [10, 20, 30, 10], res = 2
C++
#include <iostream>
#include <queue>
using namespace std;
void enqueue(queue<int> &q, int k)
{
q.push(k);
}
int findFrequency(queue<int> &q, int k)
{
int res = 0;
int s = q.size();
while (s)
{
s--;
int x = q.front();
q.pop();
if (x == k)
{
res++;
}
q.push(x);
}
return res;
}
int main()
{
queue<int> q;
int n = 8;
int insert[n] = {1, 2, 3, 4, 5, 2, 3, 1};
int m = 5;
int findFreq[m] = {1, 3, 2, 9, 10};
for (int i = 0; i < n; i++)
{
enqueue(q, insert[i]);
}
for (int i = 0; i < m; i++)
{
int f = findFrequency(q, findFreq[i]);
if (f != 0)
{
cout << f << "\n";
}
else
{
cout << "-1\n";
}
}
return 0;
}
Java
// Importing necessary libraries
import java.util.LinkedList;
import java.util.Queue;
// Function to enqueue an element
public static void enqueue(Queue<Integer> q, int k) {
q.add(k);
}
// Function to find frequency of k in the queue
public static int findFrequency(Queue<Integer> q, int k) {
int res = 0;
int s = q.size();
for (int i = 0; i < s; i++) {
int x = q.poll();
if (x == k) {
res++;
}
q.add(x);
}
return res;
}
// Main function
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
int n = 8;
int[] insert = {1, 2, 3, 4, 5, 2, 3, 1};
int m = 5;
int[] findFreq = {1, 3, 2, 9, 10};
for (int i = 0; i < n; i++) {
enqueue(q, insert[i]);
}
for (int i = 0; i < m; i++) {
int f = findFrequency(q, findFreq[i]);
if (f != 0) {
System.out.println(f);
} else {
System.out.println(-1);
}
}
}
Python
#include <iostream>
#include <queue>
# Function to enqueue an element
def enqueue(q, k):
q.append(k)
# Function to find frequency of k in the queue
def find_frequency(q, k):
res = 0
s = len(q)
for _ in range(s):
x = q.pop(0)
if x == k:
res += 1
q.append(x)
return res
# Main function
if __name__ == '__main__':
q = []
n = 8
insert = [1, 2, 3, 4, 5, 2, 3, 1]
m = 5
find_freq = [1, 3, 2, 9, 10]
for i in range(n):
enqueue(q, insert[i])
for i in range(m):
f = find_frequency(q, find_freq[i])
if f != 0:
print(f)
else:
print(-1)
C#
// Importing necessary libraries
using System;
using System.Collections.Generic;
class Program {
// Function to enqueue an element
public static void Enqueue(Queue<int> q, int k) {
q.Enqueue(k);
}
// Function to find frequency of k in the queue
public static int FindFrequency(Queue<int> q, int k) {
int res = 0;
int s = q.Count;
for (int i = 0; i < s; i++) {
int x = q.Dequeue();
if (x == k) {
res++;
}
q.Enqueue(x);
}
return res;
}
// Main function
public static void Main() {
Queue<int> q = new Queue<int>();
int n = 8;
int[] insert = {1, 2, 3, 4, 5, 2, 3, 1};
int m = 5;
int[] findFreq = {1, 3, 2, 9, 10};
for (int i = 0; i < n; i++) {
Enqueue(q, insert[i]);
}
for (int i = 0; i < m; i++) {
int f = FindFrequency(q, findFreq[i]);
if (f != 0) {
Console.WriteLine(f);
} else {
Console.WriteLine(-1);
}
}
}
}
JavaScript
// Function to enqueue an element
function enqueue(q, k) {
q.push(k);
}
// Function to find frequency of k in the queue
function findFrequency(q, k) {
let res = 0;
let s = q.length;
for (let i = 0; i < s; i++) {
let x = q.shift();
if (x === k) {
res++;
}
q.push(x);
}
return res;
}
// Main function
const q = [];
const n = 8;
const insert = [1, 2, 3, 4, 5, 2, 3, 1];
const m = 5;
const findFreq = [1, 3, 2, 9, 10];
for (let i = 0; i < n; i++) {
enqueue(q, insert[i]);
}
for (let i = 0; i < m; i++) {
let f = findFrequency(q, findFreq[i]);
if (f !== 0) {
console.log(f);
} else {
console.log(-1);
}
}
Time Complexity: O(m + n)
Auxiliary Space: O(n)
Similar Reads
Create Linked List Frequency
Given the head of a linked list containing k distinct elements, the task is to return the head of linked list of length k containing the frequency of each distinct element in the given linked list in any order. Example: Input: head = {1,2,1,2,3}Output: {2,2,1}Explanation: There are 3 distinct elemen
6 min read
FIFO Principle of Queue
FIFO stands for "First In, First Out". This principle dictates that the first element added to the queue is the first one to be removed. Understanding the FIFO principle is crucial for anyone working in computer science, especially when dealing with queues. It ensures that the first task or item you
2 min read
Queue meaning in DSA
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in the First In First Out (FIFO) order. Queue Data StructureCharacteristics of Queue:The first item added to the queue is the first one to be processed (or can be firstly deleted/removed), and su
3 min read
Difference between Queue and Deque in C++
Queue: A Queue is a linear data structure that follows a First In First Out (FIFO) order in which the operations are performed. It is a type of container adaptor where elements are inserted into one end of the container and deleted from the other. Functions: empty(): Tests whether the queue is empty
4 min read
Circular Queue in Python
A Circular Queue is a kind of queue that can insert elements inside it dynamically. Suppose, in a given array there is not any space left at the rear but we have space at the front in the normal queue it is not possible to insert elements at the front but in the case of a circular queue we can do th
3 min read
Queue Implementation in Python
Queue is a linear data structure that stores items in a First In First Out (FIFO) manner. With a queue, the least recently added item is removed first. One can imagine a queue as a line of people waiting to receive something in sequential order which starts from the beginning of the line. It is an o
4 min read
Fill and Empty a Queue
Given an empty queue and an array of elements, we need to perform the following two operations.1. Filling a QueueFilling a queue refers to the process of adding array elements to it. In a queue, elements are added to the rear or back of the queue.Insert Elements: You begin by inserting elements into
4 min read
Priority Queue in Python
A priority queue is like a regular queue, but each item has a priority. Instead of being served in the order they arrive, items with higher priority are served first. For example, In airlines, baggage labeled âBusinessâ or âFirst Classâ usually arrives before the rest. Key properties of priority que
3 min read
implement k Queues in a single array
Given an array of size n, the task is to implement k queues using the array.enqueue(qn, x) : Adds the element x into the queue number qn dequeue(qn, x) : Removes the front element from queue number qn isFull(qn) : Checks if the queue number qn is fullisEmpty(qn) : Checks if the queue number qn is em
15+ min read
Queue in Go Language
A queue is a linear structure that follows a particular order in which the operations are performed. The order is First In First Out (FIFO). Now if you are familiar with other programming languages like C++, Java, and Python then there are inbuilt queue libraries that can be used for the implementat
4 min read