Browse Curriculum

Frequency Table

A specific application of Hash Maps used to count the occurrences of each element in a collection.

h
e
l
l
o
w
o
r
l
d

Frequencies will appear here...

See the Logic in Motion
Stop memorizing code. Unlock the full interactive visualizer to master the logic step-by-step.
Unlock VisualizerPREMIUM FEATURE

Intuition

Imagine you are tallying votes in an election. You read each ballot name and add a tally mark next to that candidate's name on a chalkboard. If the name isn't on the board yet, you write it down with one mark. This chalkboard is your Frequency Table.

Concept

A Frequency Table is typically implemented using a Hash Map where:

  • Key: The element (e.g., a character or number).
  • Value: The count of how many times that element has appeared so far.

How it Works

Building the Table:

  1. Initialize an empty Hash Map.
  2. Iterate through the input array or string.
  3. For each element:
    • If it exists in the map, increment its value (count + 1).
    • If it doesn't exist, add it to the map with value 1.

Step-by-Step Breakdown

Watch the visualization:

  • Input Processing: The algorithm steps through the string one character at a time.
  • Table Update: If the character is new, a new entry appears. If it exists, the count increments.
  • Result: The final table shows the distribution of all elements.

When to Use

Applications:

  • Finding the most frequent element (Mode).
  • Checking if two strings are Anagrams (same character counts).
  • Sorting elements by frequency.
  • Data compression (Huffman Coding).

When NOT to Use

  • Sorted Data: If data is already sorted, you can count frequencies in O(1) space by iterating.
  • Small Range: If counting integers in a small range (e.g., 0-100), a simple array is faster than a Hash Map.

How to Identify

"Count occurrences", "Most frequent", "Anagram", "Majority Element".

Stop Guessing, Start Mastering.

Build the FAANG intuition. Master this pattern with optimized implementations, visual dry runs, and our curated collection of high-yield problems.

Start Your Premium Prep