Quadratic Removal¶
Rule details
| Rule ID | quadratic-removal |
| Severity | WARNING — likely performance problem |
| Confidence | MEDIUM — likely correct, some context-dependent |
| Category | Loop amplifiers |
| Complexity | O(n²) → O(n) |
Description¶
Detects remove() calls on lists inside loops. Each ArrayList.remove() shifts all subsequent elements one position, making it O(n) per removal. In a loop, this compounds to O(n²).
This rule excludes Map.remove() (O(1) for HashMap), Set.remove() (O(1) for HashSet), Iterator.remove() (safe), and removeFirst()/removeLast() (which indicate Queue/Deque usage where removal is O(1)).
Typical¶
After¶
Suggestion¶
Use removeAll() with a Set, Iterator.remove(), or filter into a new collection