ContainsDuplicates in AP Computer Science A

containsDuplicates is a helper method in AP Computer Science A that returns true if a given array (often a row or column pulled from a 2D array) holds any repeated values; it appears in 2D array traversal problems like the 2018 FRQ Q4 (ArrayTester), where credited solutions call it instead of rewriting it.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is containsDuplicates?

containsDuplicates is a boolean helper method that answers one question. Does this array contain the same value more than once? In AP CSA, you usually meet it inside a 2D array problem. A row is already a 1D array, and a column can be extracted into one, so checking a whole grid for duplicates breaks down into running containsDuplicates on each row and each column.

That breakdown is the real lesson. Instead of writing one giant nested-loop monster that scans the entire 2D array at once, you reduce the 2D problem to a series of 1D checks. This matches how the CED frames 2D arrays in Topic 4.12: a 2D array is stored as an array of arrays, so traversal logic you already know from 1D arrays carries over directly.

Why containsDuplicates matters in AP® Computer Science A

containsDuplicates lives in Unit 4 (Data Collections), Topic 4.12 (Traversing 2D Arrays), and supports learning objective 4.12.A, which asks you to develop code that traverses 2D array elements and determine the results of those traversals. Per EK 4.12.A.1, nested iteration handles 2D traversal in row-major or column-major order, and helper methods like containsDuplicates are how exam problems keep that traversal manageable. The bigger skill it tests is procedural decomposition. The College Board loves giving you a small helper and checking whether you trust it enough to call it, because that's exactly how real programs are built.

How containsDuplicates connects across the course

Traversal (Unit 4)

containsDuplicates is a traversal with a purpose. Under the hood it visits elements (typically comparing each element against the ones after it), which is the same nested-loop access pattern EK 4.12.A.1 describes for moving through array elements in order.

Row-major vs. column-major order (Unit 4)

Checking rows for duplicates is a row-major idea, and checking columns is a column-major idea. containsDuplicates lets you do both with one method, as long as you can hand it the right 1D array each time.

The getColumn helper pattern (Unit 4)

On the 2018 FRQ Q4, getColumn pulls one column of a 2D array into a 1D array. Pair it with containsDuplicates and a column check becomes trivial. Extract the column, then ask if it has repeats. That two-helper combo is the FRQ in a nutshell.

Is containsDuplicates on the AP® Computer Science A exam?

containsDuplicates appeared on the 2018 FRQ Q4 (ArrayTester), a classic 2D array question. The setup gives you helpers, including a method to grab a column as a 1D array, and then asks you to verify properties of the grid. The credited approach calls the provided helpers: loop over the rows and columns, pass each one to containsDuplicates, and return false the moment any check fails. The single biggest scoring trap is re-implementing the helper. If the problem gives you containsDuplicates, writing your own duplicate-checking loops wastes time and risks errors that cost points. In multiple choice, the same idea shows up as code-tracing questions where you determine what a duplicate-checking traversal returns for a given 2D array, which is exactly the skill named in 4.12.A.

ContainsDuplicates vs getColumn

Both are helpers from the 2018 ArrayTester FRQ, but they do opposite jobs. getColumn builds something (it returns a 1D array holding one column of a 2D array), while containsDuplicates judges something (it returns a boolean saying whether a 1D array has repeats). On the FRQ they chain together. getColumn produces the array, and containsDuplicates inspects it. If you mix up which one returns an array and which returns a boolean, your method calls won't compile.

Key things to remember about containsDuplicates

  • containsDuplicates is a boolean helper method that returns true if an array contains any repeated values, usually applied to a row or column of a 2D array.

  • It supports Topic 4.12 and learning objective 4.12.A, which is about developing 2D array traversal code and determining what it produces.

  • Because a 2D array is an array of arrays (EK 4.12.A.1), each row is already a 1D array you can pass straight to containsDuplicates, and getColumn gives you each column as a 1D array.

  • On the 2018 FRQ Q4, full-credit solutions called the provided containsDuplicates helper instead of rewriting the duplicate-checking logic from scratch.

  • Checking every row for duplicates uses row-major thinking, and checking every column uses column-major thinking, so this one helper exercises both traversal orders from the CED.

Frequently asked questions about containsDuplicates

What is containsDuplicates in AP Computer Science A?

It's a helper method that returns true if an array holds the same value more than once. It's best known from the 2018 FRQ Q4 (ArrayTester), where it checks rows and columns of a 2D array, the core skill of Topic 4.12 in Unit 4.

Do I have to write the containsDuplicates method myself on the AP exam?

Usually no. On the 2018 FRQ it was provided, and you earned points by calling it correctly. Rewriting a provided helper is one of the most common ways to lose time and points on 2D array FRQs.

How is containsDuplicates different from getColumn?

getColumn returns a 1D array (one column of a 2D array), while containsDuplicates returns a boolean (whether an array has repeats). On the 2018 FRQ they work as a pipeline. getColumn extracts the column, then containsDuplicates checks it.

Does containsDuplicates work on a 2D array directly?

No, it takes a 1D array. The trick is that rows of a 2D array are already 1D arrays, and columns become 1D arrays once you extract them with a helper like getColumn. That's how a 2D duplicate check reduces to repeated 1D checks.

How would containsDuplicates check a whole 2D array, like a Latin square problem?

Loop through every row and call containsDuplicates on it, then loop through every column index, extract each column with getColumn, and call containsDuplicates on that. If any call returns true, the grid fails the check and you return false immediately.