2671. Frequency Tracker LeetCode Solution
In this guide, you will get 2671. Frequency Tracker LeetCode Solution with the best time and space complexity. The solution to Frequency Tracker problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Frequency Tracker solution in C++
- Frequency Tracker solution in Java
- Frequency Tracker solution in Python
- Additional Resources

Problem Statement of Frequency Tracker
Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.
Implement the FrequencyTracker class.
FrequencyTracker(): Initializes the FrequencyTracker object with an empty array initially.
void add(int number): Adds number to the data structure.
void deleteOne(int number): Deletes one occurrence of number from the data structure. The data structure may not contain number, and in this case nothing is deleted.
bool hasFrequency(int frequency): Returns true if there is a number in the data structure that occurs frequency number of times, otherwise, it returns false.
Example 1:
Input
[“FrequencyTracker”, “add”, “add”, “hasFrequency”]
[[], [3], [3], [2]]
Output
[null, null, null, true]
Explanation
FrequencyTracker frequencyTracker = new FrequencyTracker();
frequencyTracker.add(3); // The data structure now contains [3]
frequencyTracker.add(3); // The data structure now contains [3, 3]
frequencyTracker.hasFrequency(2); // Returns true, because 3 occurs twice
Example 2:
Input
[“FrequencyTracker”, “add”, “deleteOne”, “hasFrequency”]
[[], [1], [1], [1]]
Output
[null, null, null, false]
Explanation
FrequencyTracker frequencyTracker = new FrequencyTracker();
frequencyTracker.add(1); // The data structure now contains [1]
frequencyTracker.deleteOne(1); // The data structure becomes empty []
frequencyTracker.hasFrequency(1); // Returns false, because the data structure is empty
Example 3:
Input
[“FrequencyTracker”, “hasFrequency”, “add”, “hasFrequency”]
[[], [2], [3], [1]]
Output
[null, false, null, true]
Explanation
FrequencyTracker frequencyTracker = new FrequencyTracker();
frequencyTracker.hasFrequency(2); // Returns false, because the data structure is empty
frequencyTracker.add(3); // The data structure now contains [3]
frequencyTracker.hasFrequency(1); // Returns true, because 3 occurs once
Constraints:
1 <= number <= 105
1 <= frequency <= 105
At most, 2 * 105 calls will be made to add, deleteOne, and hasFrequency in total.
Complexity Analysis
- Time Complexity: O(1)
- Space Complexity: O(|\texttt{add()}|)
2671. Frequency Tracker LeetCode Solution in C++
class FrequencyTracker {
public:
void add(int number) {
if (count[number] > 0)
--freqCount[count[number]];
++count[number];
++freqCount[count[number]];
}
void deleteOne(int number) {
if (count[number] == 0)
return;
--freqCount[count[number]];
--count[number];
++freqCount[count[number]];
}
bool hasFrequency(int frequency) {
return freqCount[frequency] > 0;
}
private:
unordered_map<int, int> count;
unordered_map<int, int> freqCount;
};
/* code provided by PROGIEZ */
2671. Frequency Tracker LeetCode Solution in Java
class FrequencyTracker {
public void add(int number) {
if (count[number] > 0)
--freqCount[count[number]];
++count[number];
++freqCount[count[number]];
}
public void deleteOne(int number) {
if (count[number] == 0)
return;
--freqCount[count[number]];
--count[number];
++freqCount[count[number]];
}
public boolean hasFrequency(int frequency) {
return freqCount[frequency] > 0;
}
private int[] freqCount = new int[100_001];
private int[] count = new int[100_001];
}
// code provided by PROGIEZ
2671. Frequency Tracker LeetCode Solution in Python
class FrequencyTracker:
def __init__(self):
self.count = collections.Counter()
self.freqCount = collections.Counter()
def add(self, number: int) -> None:
if self.count[number] > 0:
self.freqCount[self.count[number]] -= 1
self.count[number] += 1
self.freqCount[self.count[number]] += 1
def deleteOne(self, number: int) -> None:
if self.count[number] == 0:
return
self.freqCount[self.count[number]] -= 1
self.count[number] -= 1
self.freqCount[self.count[number]] += 1
def hasFrequency(self, frequency: int) -> bool:
return self.freqCount[frequency] > 0
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.