Open In App

Frequency in Queue

Last Updated : 26 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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);
    }
}

Output
2 2 2 -1 -1 

Time Complexity: O(m + n)
Auxiliary Space: O(n)


Next Article
Practice Tags :

Similar Reads