Queue Operations | Practice | GeeksforGeeks

Here, we will learn operations on queues.
Given N integers, the task is to insert those elements in the queue. Also, given M integers, task is to find the frequency of each number in the Queue.
Example:
Input:
8
1 2 3 4 5 2 3 1
5
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).
Your Task:
Your task is to complete the functions insert() and findFrequency() which inserts the element into the queue and find the count of occurences of element in the queue respectively.
Constraints:
1 <= N <= 103
1 <= M <= 103
1 <= Elements of Queue <= 106
0
harshscode2 weeks ago
  void insert(queue<int> &q, int k){       q.push(k);       m[k]++;   }      // Function to find frequency of an element   // return the frequency of k   int findFrequency(queue<int> &q, int k){                             return (m[k]==0)?(-1):(m[k]);         }   
+1
goyalayushi4522 months ago
class Solution{
   public:
   map<int,int>m;
   void insert(queue<int> &q, int k){
     q.push(k);
     m[k]++;
   }
   
   int findFrequency(queue<int> &q, int k){
       return (m[k] == 0)? -1 : m[k];
   }
};

0
kalikisiva0072 months ago
class Solution{   public:   // Function to insert element into the queue   void insert(queue<int> &q, int k){       // Your code here      q.push(k);   }      // Function to find frequency of an element   // return the frequency of k   int findFrequency(queue<int> &q, int k){       // Your code here       vector<int>v;       int p =0;       while(q.size()!=0){           if(k== q.front()){               p++;           }           v.push_back(q.front());           q.pop();       }       for(int i =0;i<v.size();i++){           q.push(v[i]);       }       return p;
0
sdmrf2 months ago
    def insert(self, q, k):       q.append(k)       return q
    def findFrequency(self, q, k):       return q.count(k)
 
Python Soln
-3
dhirunand3 months ago
class Geeks{
    static void insert(Queue<Integer> q, int k){
        q.offer(k);
    }
    
    static int findFrequency(Queue<Integer> q, int k){
        int count = 0;
        
        int peek;
        int size = q.size();
        for(int i = 0; i< size; i++){
            peek=q.poll();
            if(peek==k)
                count++;
            q.offer(peek);
        }
 
        return count;   
    }
}
0
aroopghosh5533 months ago
class Solution{
    public:
    
    void insert(queue<int> &q, int k){
        q.push(k);
    }
    unordered_map<int, int> mp;
    int findFrequency(queue<int> &q, int k){
        while(!q.empty()){
            mp[q.front()]++;
            q.pop();
        }
        if(mp.find(k) == mp.end()){
            return -1;
        }
        return mp[k];
    }
    
};
0
firosk7
This comment was deleted.
0
avinav26113 months ago
Easy C++ Solution
 
0
rahulsingh329293 months ago
def insert(q,n): q.append(n) return q  
def findFrequency(arr,i): if i in arr:   return arr.count(i) return -1 
+2
goelr5643 months ago
class Solution{   public:   void insert(queue<int> &q, int k){       q.push(k);   }      int findFrequency(queue<int> &q, int k){       queue<int> temp=q;
//ek temporary queue bna le sare changes pop wagra usme krio bhai taki main m bar bar ek number ki tb frequency dekhe toh vo change na krni pde.....       int freq=0;       while(!temp.empty()) //matlab last tk dekha ki khi element h toh nhi jo dekhna h       {           if(k==temp.front())           freq++;           temp.pop();       }       return freq;   }
 
// pura mat chap khud likh samaj samj k
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.