Compare every item to every other item in ArrayList
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you need to compare every item in an ArrayList with every other item, the usual solution is a nested loop. The important detail is where the inner loop starts, because that determines whether you compare each pair once, compare items with themselves, or compare the same pair twice.
Most of the time, you want unique unordered pairs. In that case, start the inner loop at i + 1. That avoids self-comparisons and duplicate work.
Compare Each Pair Exactly Once
Here is the standard pattern:
Output:
That is the cleanest version when order does not matter. You see each pair once and never compare an item with itself.
Why j = i + 1 Matters
Suppose the inner loop started at 0 instead:
Now you get:
- self-comparisons such as
AversusA - duplicate comparisons such as
AversusBand laterBversusA
That may be correct if direction matters, but it is wasteful for symmetric comparisons like equality checks or distance calculations.
A Reusable Generic Method
If you do this often, wrap the pattern in a helper method:
Usage:
This makes the comparison logic reusable without repeating nested loops everywhere in your codebase.
Complexity and When It Becomes Expensive
Comparing every item to every other item takes O(n²) time. That cost grows quickly:
- 10 items gives 45 unique pairs
- 100 items gives 4,950 unique pairs
- 10,000 items gives almost 50 million unique pairs
So while the nested-loop solution is correct, it may be too slow for large lists. If your real goal is something narrower such as finding duplicates or nearest values, there may be better algorithms using hashing, sorting, or indexing.
When You Actually Need All Ordered Pairs
Sometimes order matters. For example, if you are comparing "source" to "target" and A -> B is different from B -> A, then starting from 0 may be appropriate. In that case, explicitly skip self-comparisons:
The right loop structure depends on what "compare" means in your problem.
Common Pitfalls
- Starting the inner loop at
0when you only need unique unordered pairs. - Forgetting that the algorithm is
O(n²)and then using it on very large lists. - Modifying the
ArrayListwhile iterating through it, which can lead to logic bugs or concurrent modification problems elsewhere. - Using
equalswhen the real comparison rule is custom and should be written explicitly. - Assuming
ArrayListis the issue. The pairwise pattern itself is what makes the algorithm expensive.
Summary
- Use nested loops to compare every item in an
ArrayListwith every other item. - Start the inner loop at
i + 1when you want each unique pair exactly once. - Start at
0only if ordered pairs matter, and skipi == jif needed. - Expect quadratic runtime for full pairwise comparison.
- If performance matters, step back and ask whether you truly need all pairs or only a more specific result.

