Unit 4 Cycle 2 Day 16: Array: Frequency Table

Unit 4 Advanced (Cycle 2) Day 16 of 28 Advanced

Array: Frequency Table

Section 4.3 — Common Array Algorithms

Key Concept

A frequency table uses an array where each index represents a value and the element at that index counts how many times that value appears. For values 0-9, create int[] freq = new int[10] and increment freq[value] for each occurrence. This technique is useful for counting character frequencies, digit distributions, and score tallies. The AP exam tests frequency tables as an alternative to nested loop counting, and asks about the relationship between the input data and the resulting frequency array.

Consider the following code segment.

int[] data = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3}; int[] freq = new int[10]; for (int val : data) { freq[val]++; } System.out.println(freq[1] + " " + freq[5]);

What is printed?

Answer: (A) 2 2

1 appears twice, 5 appears twice. freq[1]=2, freq[5]=2.

Why Not the Others?

(B) These are values, not frequencies.

(C) 5 appears twice, not once.

(D) 1 appears twice, not once.

Common Mistake

Frequency array: index = value, element = count. freq[val]++ counts each occurrence.

AP Exam Tip

Frequency arrays work when values fall in a known integer range. The index represents the value being counted.

Review this topic: Section 4.3 — Common Array Algorithms • Unit 4 Study Guide
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.