Standard Algorithms on ArrayList

Part of: ArrayList

Library of Patterns The AP CSA exam expects you to write common list algorithms by hand on an ArrayList. These all build from get, set, size, and loops. Master these patterns. Finding a Maximum or Minimum Assume the first element is the best, then improve: Searching A linear search returns the index of a target, or -1 if absent: For objects like Strings, use .equals() instead of ==. Counting and Summing Building a New List Many problems filter or transform into a fresh list rather than mutating the original: Detecting Duplicates A nested loop compares each pair: Summary - Max/min: seed with the first element, then compare. - Linear search: return index or -1; use .equals() for objects. - Count/sum: accumulate in a loop. - Filter/transform: append into a new list. - Duplicates: nested loops with i and j = i+1. These reusable patterns are the heart of free-response questions.

Challenge: Find Max and Its Index