Class FrequencyTracker

java.lang.Object
g2601_2700.s2671_frequency_tracker.FrequencyTracker

public class FrequencyTracker extends Object
2671 - Frequency Tracker.<p>Medium</p> <p>Design a data structure that keeps track of the values in it and answers some queries regarding their frequencies.</p> <p>Implement the <code>FrequencyTracker</code> class.</p> <ul> <li><code>FrequencyTracker()</code>: Initializes the <code>FrequencyTracker</code> object with an empty array initially.</li> <li><code>void add(int number)</code>: Adds <code>number</code> to the data structure.</li> <li><code>void deleteOne(int number)</code>: Deletes <strong>one</strong> occurrence of <code>number</code> from the data structure. The data structure <strong>may not contain</strong> <code>number</code>, and in this case nothing is deleted.</li> <li><code>bool hasFrequency(int frequency)</code>: Returns <code>true</code> if there is a number in the data structure that occurs <code>frequency</code> number of times, otherwise, it returns <code>false</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><strong>Input</strong> [&ldquo;FrequencyTracker&rdquo;, &ldquo;add&rdquo;, &ldquo;add&rdquo;, &ldquo;hasFrequency&rdquo;] [ [], [3], [3], [2]]</p> <p><strong>Output:</strong> [null, null, null, true]</p> <p><strong>Explanation:</strong></p> <pre><code> 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 </code></pre> <p><strong>Example 2:</strong></p> <p><strong>Input</strong> [&ldquo;FrequencyTracker&rdquo;, &ldquo;add&rdquo;, &ldquo;deleteOne&rdquo;, &ldquo;hasFrequency&rdquo;] [ [], [1], [1], [1]]</p> <p><strong>Output:</strong> [null, null, null, false]</p> <p><strong>Explanation:</strong></p> <pre><code> 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 </code></pre> <p><strong>Example 3:</strong></p> <p><strong>Input</strong> [&ldquo;FrequencyTracker&rdquo;, &ldquo;hasFrequency&rdquo;, &ldquo;add&rdquo;, &ldquo;hasFrequency&rdquo;] [ [], [2], [3], [1]]</p> <p><strong>Output:</strong> [null, false, null, true]</p> <p><strong>Explanation:</strong></p> <pre><code> 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 </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= number <= 10<sup>5</sup></code></li> <li><code>1 <= frequency <= 10<sup>5</sup></code></li> <li>At most, <code>2 * 10<sup>5</sup></code> calls will be made to <code>add</code>, <code>deleteOne</code>, and <code>hasFrequency</code> in <strong>total</strong>.</li> </ul>
  • Constructor Details

    • FrequencyTracker

      public FrequencyTracker()
  • Method Details

    • add

      public void add(int number)
    • deleteOne

      public void deleteOne(int number)
    • hasFrequency

      public boolean hasFrequency(int frequency)