Frequency Queries HackerRank Solution: Looking for Frequency Queries solution for Hackerrank problem? Get solution with source code and detailed explainer video.

You are given q queries. Each query is of the form two integers described below:
1 x : Insert x in your data structure.
2 y : Delete one occurence of y from your data structure, if present.
3 z : Check if any integer is present whose frequency is exactly z. If yes, print 1 else 0.
Go to problem statement

## Explanation Video:

Mentor: Satyendra Jaiswal
Langauge: Python

## Source Code:  for `freqQuery` function

``````
# Complete the freqQuery function below.
def freqQuery(queries):
res=[]
dic=Counter()  #d1
dic_val=Counter()  #d2
for op, value in queries:
if op==1:
dic_val[dic[value]]-=1
dic[value]+=1
dic_val[dic[value]]+=1
elif op==2:
if dic[value]>0:
dic_val[dic[value]]-=1
dic[value]-=1
dic_val[dic[value]]+=1
else:
res.append(1 if dic_val[value] > 0 else 0)
return res
``````
coming soon…
coming soon…

## Full Source Code: Frequency Queries  HackerRank Solution

``````
#!/bin/python3

import math
import os
import random
import re
import sys
from collections import Counter
# Complete the freqQuery function below.
def freqQuery(queries):
res=[]
dic=Counter()  #d1
dic_val=Counter()  #d2
for op, value in queries:
if op==1:
dic_val[dic[value]]-=1
dic[value]+=1
dic_val[dic[value]]+=1
elif op==2:
if dic[value]>0:
dic_val[dic[value]]-=1
dic[value]-=1
dic_val[dic[value]]+=1
else:
res.append(1 if dic_val[value] > 0 else 0)
return res

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

q = int(input().strip())

queries = []

for _ in range(q):
queries.append(list(map(int, input().rstrip().split())))

ans = freqQuery(queries)

fptr.write('\n'.join(map(str, ans)))
fptr.write('\n')

fptr.close()

``````
coming soon…
coming soon…