AP® Computer Science A Data Collections FRQ
Work through AP Computer Science A free-response questions on Data Collections, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Arraylists Clear filter
Question 32
Question 33
Question 34
Question 35
Question 36
Question 37
Question 38
Question 39
Question 40
An online community platform stores user posts as strings in an ArrayList. The moderation team needs tools to clean up duplicate content and identify trending topics. You are writing two analysis methods for the MessageBoard class.
PART A
Write the method public ArrayList<String> removeDuplicates(ArrayList<String> posts) that returns a new ArrayList containing only the first occurrence of each unique string, in original order.
For example, if posts is ["hello", "world", "hello", "java", "world", "hello"], the method returns ["hello", "world", "java"].
public ArrayList<String> removeDuplicates(ArrayList<String> posts) {PART B
Write the method public String mostFrequent(ArrayList<String> posts) that returns the string appearing most often. If there is a tie, return the one that appears first in the list.
For example, if posts is ["cat", "dog", "cat", "bird", "dog", "cat"], the method returns "cat" (appears 3 times).
public String mostFrequent(ArrayList<String> posts) {Main Solution
PART A — removeDuplicates
The goal is to iterate through posts and add each string to a new ArrayList only if it has not already been added. This preserves the first occurrence and maintains original order.
public ArrayList<String> removeDuplicates(ArrayList<String> posts) {
ArrayList<String> result = new ArrayList<String>();
for (String post : posts) {
if (!result.contains(post)) {
result.add(post);
}
}
return result;
}Trace with example: posts = ["hello", "world", "hello", "java", "world", "hello"]
- "hello" → not in result → add → result = ["hello"]
- "world" → not in result → add → result = ["hello", "world"]
- "hello" → already in result → skip
- "java" → not in result → add → result = ["hello", "world", "java"]
- "world" → already in result → skip
- "hello" → already in result → skip
Returns ["hello", "world", "java"] ✓
PART B — mostFrequent
The goal is to find the string with the highest frequency. In case of a tie, the one that appears first in the list wins. We iterate through each unique string (using the result of removeDuplicates or a manual check), count its occurrences, and track the maximum.
public String mostFrequent(ArrayList<String> posts) {
if (posts == null || posts.size() == 0) {
return null;
}
String mostFreq = posts.get(0);
int maxCount = 0;
// Count occurrences of each string in original order
ArrayList<String> seen = new ArrayList<String>();
for (String post : posts) {
if (!seen.contains(post)) {
seen.add(post);
int count = 0;
for (String s : posts) {
if (s.equals(post)) {
count++;
}
}
if (count > maxCount) {
maxCount = count;
mostFreq = post;
}
}
}
return mostFreq;
}Trace with example: posts = ["cat", "dog", "cat", "bird", "dog", "cat"]
- "cat": count = 3 → maxCount = 3, mostFreq = "cat"
- "dog": count = 2 → 2 < 3, no update
- "bird": count = 1 → 1 < 3, no update
Returns "cat" ✓
Tie-breaking note: Because we iterate through posts in order and only update mostFreq when count > maxCount (strict greater-than), the first string encountered with the highest count is retained, satisfying the tie-breaking requirement.
Teacher Explanation
Part A tests the student's ability to build a new collection by filtering duplicates using ArrayList.contains() and a for-each loop. Key scoring points: (1) creates and returns a new ArrayList, (2) uses contains or equivalent logic to check for prior occurrence, (3) adds only on first occurrence, (4) preserves original order.
Part B tests nested iteration to count frequencies and track a running maximum. Key scoring points: (1) correctly counts occurrences of each string, (2) uses strict > (not >=) to preserve first-occurrence tie-breaking, (3) returns the correct string, (4) handles the full list without skipping elements.
Education Coach Advice
AP Exam Strategy:
- For Part A, always create a new ArrayList and return it — never modify the parameter directly unless the prompt explicitly says to.
- For Part B, write out a small trace (like the example given) to verify your tie-breaking logic before finalizing your answer.
- Use
.equals()for all String comparisons in Java — never==. - Read the tie-breaking rule carefully: "first in the list" means use strict
>, not>=. - If you use a helper method (like calling
removeDuplicatesinsidemostFrequent), make sure it is correctly implemented — errors propagate.
Traps
Part A traps:
- Modifying the original
postslist instead of creating a new one — loses points for not returning a new ArrayList. - Using
==instead of.equals()for String comparison — will fail for non-interned strings. - Forgetting to return the result list.
Part B traps:
- Using
>=instead of>when updating the maximum — this would return the last string with the highest count instead of the first, violating the tie-breaking rule. - Counting each string multiple times (not tracking which strings have already been counted) — leads to redundant work but may still produce correct output; however, it risks off-by-one logic errors.
- Returning
nullor an empty string when the list is non-empty.
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

