Using array indices as buckets.
When values are small non-negative integers, the cleanest counter is another array indexed by those values.
A frequency array uses the array's indices as bins. To count how often each value appears, increment counts[arr[i]] for every element. The result: counts[v] is the frequency of value v.
This works when values are small, non-negative integers — like grades 0-100, dice rolls 1-6, or letter indices 0-25. For arbitrary values, use parallel lists or an ArrayList of objects instead.
On the AP exam, this pattern is the most elegant way to handle "count how many of each" questions.
Allocate a counter array; increment by value.
Each index holds the count of its corresponding value.
Basic frequency count
public int[] frequencies(int[] arr, int maxValue) {
int[] counts = new int[maxValue + 1];
for (int v : arr) {
counts[v]++;
}
return counts;
}
The counter array's length is maxValue + 1 so the highest value has a valid index.
Mode (most frequent value)
int mode = 0;
for (int v = 1; v < counts.length; v++) {
if (counts[v] > counts[mode]) {
mode = v;
}
}
The mode is the index — not the count. The count is the value at that index.
Counting characters
int[] charCounts = new int[26];
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
charCounts[c - 'a']++;
}
Subtracting 'a' maps each lowercase letter to index 0-25.
Frequency-array vocabulary.
Bin-counting terminology.
- Frequency array — counter indexed by value.
- Bin / bucket — one slot in the frequency array.
- Mode — value with highest frequency.
- Histogram — visual form of a frequency array.
- Index-as-value — using the index to represent the value being counted.
How frequency arrays are tested.
Sizing and value-vs-index confusion dominate.
- Size as
maxValue + 1. Off by one is common. - Mode is the index. Its frequency is the value at that index.
- Non-negative values only. Negative indices throw exceptions.
- Sum of all counts equals array length. Useful sanity check.
Count dice rolls.
Six bins; one pass.
Input: [3, 1, 4, 1, 5, 1, 2, 6], max value 6.
Counter array (length 7):
- counts[1] = 3 (three 1s)
- counts[2] = 1
- counts[3] = 1
- counts[4] = 1
- counts[5] = 1
- counts[6] = 1
- counts[0] = 0
Mode: index 1 (value 1 appeared 3 times).
Sanity check: sum of counts = 3+1+1+1+1+1 = 8 = array length.
Frequency-array pitfalls.
Most come from sizing or wrong return.
- Sizing as
maxValue. Should bemaxValue + 1. - Negative values. Crash on index -1.
- Returning the count instead of the index for mode. Read the prompt's verb.
- Forgetting
- 'a'in char-counting.
Frequency-array essentials.
Allocate, increment, query.
Frequency Reference
AP Quick Reference| Step | Operation | Notes |
|---|---|---|
| Allocate | new int[maxValue + 1] | +1 for highest index. |
| Increment | counts[v]++ | v becomes the index. |
| Query | counts[v] | Number of v's seen. |
| Mode | Argmax over counts | Returns the index. |
How to master frequency arrays.
Always size maxValue + 1; always sanity-check the sum.
- MCQ Practice — verify counter array length is
maxValue + 1. - Java Lab — count dice rolls, grades, and letters. Verify sum equals input length.
- FRQ Practice — when prompted for "how many of each", reach for the frequency array.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 07 Topic 08 Frequency Array (Counting by Index) FRQ Practice
AP-style topic practice assessment