01 / 20
\(O(n)\)

Big-O for
Collections

Understand how time complexity affects loops and common Java collection operations: from ArrayList to HashMap.

Java

What Is Big-O Notation?

Big-O notation describes the upper bound of how an algorithm's running time grows relative to the input size \( n \). It answers: "As my data gets bigger, how much slower does my code get?"

\( O(1) \)
Constant: time doesn't change with input size
\( O(n) \)
Linear: time grows directly with input size
\( O(n^2) \)
Quadratic: time grows with the square of input size

Visualizing Growth Rates

Input size (n) Time
\(O(1)\)
\(O(\log n)\)
\(O(n)\)
\(O(n \log n)\)
\(O(n^2)\)

Why Big-O Matters in Java

Java gives you many collection classes, but they have different performance characteristics. Picking the wrong one can turn a snappy app into a crawling one.

Correct Choice
Use a HashMap for lookups: \( O(1) \) average. Searching 1 million keys is instant.
Wrong Choice
Use an ArrayList for lookups by value: \( O(n) \). Searching 1 million items takes 1 million steps.

The Core Rule: Counting Loop Iterations

The simplest way to find Big-O: count how many times the innermost operation runs as a function of input size \( n \).

1
Single loop from \(0\) to \( n \): the body runs \( n \) times → \( O(n) \)
2
Nested loops, each from \(0\) to \( n \): the inner body runs \( n \times n \) times → \( O(n^2) \)
3
Loop that halves the range each time: runs \( \log_2 n \) times → \( O(\log n) \)
4
Sequential (non-nested) loops: add their complexities. \( O(n) + O(n) = O(n) \)

Loop Patterns at a Glance

\(O(1)\)
arr[0] map.get(key) No loop
\(O(\log n)\)
Binary search TreeMap.get() Halving loop
\(O(n)\)
for (int x : list) list.contains() Single loop
\(O(n^2)\)
Nested loops Bubble sort Loop in loop Slower →
for (int i = 0; i < \(n\); i++) for (int j = 0; j < \(n\); j++)
// inner body → \(n \times n = O(n^2)\)
← \(n\) iterations
← \(n\) iterations each
= \(n^2\) total

ArrayList vs LinkedList

Operation ArrayList LinkedList
get(index) \( O(1) \) \( O(n) \)
add(end) \( O(1) \) amortized \( O(1) \)
add(index, elem) \( O(n) \) \( O(n) \)
remove(index) \( O(n) \) \( O(n) \)
contains(obj) \( O(n) \) \( O(n) \)

HashMap & HashSet Power

Hash-based structures use a hash function to compute an index in an internal array, giving near-instant access.

put(key, val) / add(elem)
\( O(1) \) average
get(key) / contains(elem)
\( O(1) \) average
remove(key/elem)
\( O(1) \) average

TreeMap & TreeSet: Sorted Order

Tree-based collections keep elements in sorted order using a Red-Black tree. Every operation walks the tree height, which is \( O(\log n) \).

put / add
\( O(\log n) \)
get / contains
\( O(\log n) \)
remove
\( O(\log n) \)

How to Analyze Your Code

1
Identify the input size \( n \): usually the length of a collection or array.
2
Find the loops: how many times does each loop run? Is it \( n \), \( n/2 \), or \( \log n \)?
3
Check nested structure: nested loops multiply. Sequential loops add (keep the largest).
4
Account for collection operations inside loops: a contains() on an ArrayList inside a loop is \( O(n) \times O(n) = O(n^2) \).
5
Drop constants and lower terms: \( O(3n + 5) \) → \( O(n) \). \( O(n^2 + n) \) → \( O(n^2) \).

Example 1: Simple Loop

int sum = 0; for (int i = 0; i < list.size(); i++) { sum += list.get(i); // O(1) for ArrayList }

Example 2: Hidden \( O(n^2) \)

// Find duplicates in a list List<String> dupes = new ArrayList<>(); for (String s : list) { // O(n) if (dupes.contains(s)) { // O(n) ← hidden! System.out.println(s); } dupes.add(s); }

Example 3: Sort Then Search

Collections.sort(list); // \(O(n \log n)\) for (String target : queries) { // \(O(m)\) queries int idx = Collections.binarySearch(list, target); // \(O(\log n)\) each }

Common Mistakes

✕ Ignoring Collection Ops
Calling list.contains() or list.indexOf() inside a loop. Each is \( O(n) \), creating a hidden \( O(n^2) \).
✕ Confusing Average and Worst
HashMap is \( O(1) \) average, but assuming it's always \( O(1) \) can lead to surprises with bad hash functions.
✕ Forgetting Amortized Cost
ArrayList.add() is \( O(1) \) amortized, but a single add that triggers a resize copies the entire array: \( O(n) \).

The Big-O Cheat Sheet

Collection Access Search Insert Delete
ArrayList \( O(1) \) \( O(n) \) \( O(1) \)* \( O(n) \)
LinkedList \( O(n) \) \( O(n) \) \( O(1) \)* \( O(n) \)
HashMap \( O(1) \) \( O(1) \) \( O(1) \) \( O(1) \)
TreeMap \( O(\log n) \) \( O(\log n) \) \( O(\log n) \) \( O(\log n) \)
HashSet — \( O(1) \) \( O(1) \) \( O(1) \)

Question 1 of 5

What is the time complexity of calling get(i) on an ArrayList in Java?

Question 2 of 5

You have a loop that iterates through an ArrayList of size \( n \), and inside the loop you call list.contains(item). What is the total time complexity?

Question 3 of 5

Which Java collection provides \( O(1) \) average time for both insertion and membership testing?

Question 4 of 5

What is the overall time complexity of this code?

Question 5 of 5

A developer replaces an ArrayList with a HashSet inside a loop to check for duplicates. The overall complexity drops from \( O(n^2) \) to \( O(n) \). Why?