01
Overview

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.

02
Core Concept

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.

03
Key Vocabulary

Frequency-array vocabulary.

Bin-counting terminology.

Vocabulary
  • 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.
04
AP Exam Focus

How frequency arrays are tested.

Sizing and value-vs-index confusion dominate.

Exam Focus
  • 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.
05
Worked Example

Count dice rolls.

Six bins; one pass.

Worked Example AP-style reasoning

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.

06
Common Mistakes

Frequency-array pitfalls.

Most come from sizing or wrong return.

Watch Out
  • Sizing as maxValue. Should be maxValue + 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.
07
Reference Table

Frequency-array essentials.

Allocate, increment, query.

Frequency Reference

AP Quick Reference
StepOperationNotes
Allocatenew int[maxValue + 1]+1 for highest index.
Incrementcounts[v]++v becomes the index.
Querycounts[v]Number of v's seen.
ModeArgmax over countsReturns the index.
08
Practice Tip

How to master frequency arrays.

Always size maxValue + 1; always sanity-check the sum.

Practice Strategy
  • 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.
09
Section · 09

Practice — attempt these now.

AP-style assessments aligned to this lesson. Time them.

FRQ Practice 30 min 18 marks Pending

AP CSA Unit 07 Topic 08 Frequency Array (Counting by Index) FRQ Practice

AP-style topic practice assessment

Start