Unit 4 Cycle 2 Day 16: Array: Frequency Table
Share
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.
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.