Browse curriculum

Frequency Table

A frequency table is an application of hash maps that counts the occurrences of each element in a collection.

11 CHARS · 8 DISTINCT

NEW KEY

h

0

e

1

l

2

l

3

o

4

␣

5

w

6

o

7

r

8

l

9

d

10

Counts

0

␣

0

d

0

e

1

h

0

l

0

o

0

r

0

w

 

'h' seen for the first time — start its count at 1.

Next

'e' seen for the first time — start its count at 1.

Text

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".

Applying for software engineering roles? Free, no sign-up:

AI resume checker — match your resume to the job description

Frequently Asked Questions

What is a Frequency Table?

A Frequency Table is a specific use of a Hash Map or array to count the occurrences of elements in a data set (e.g., characters in a string).


When to use a Frequency Array?

For a small fixed range (like 26 lowercase English letters), an array counts faster and uses less memory than a full Hash Map.


Common interview uses?

Finding the first non-repeating character, checking for anagrams (valid anagram), and most frequent element problems.