Sorting +
Scanning
Master the powerful two-phase pattern: sort first, then scan linearly to solve array problems efficiently in Java.
What Is Sort + Scan?
Sort + Scan is a two-phase algorithmic pattern where you first sort an array to impose order, then make a single linear pass (scan) to extract the answer. By sorting first, relationships between elements become obvious: duplicates sit side by side, gaps become visible, and extremes move to the edges.
Why Sort First?
The Two Phases Visualized
The Pattern Template
Sort + Scan vs Brute Force
| Brute Force | Sort + Scan | |
|---|---|---|
| Approach | Nested loops: check every pair | Sort, then single pass |
| Time | \(O(n^2)\) | \(O(n \log n)\) |
| Space | \(O(1)\) | \(O(1)\) to \(O(n)\) |
| Code complexity | Simple but slow | Simple and fast |
| When \(n = 10{,}000\) | 100,000,000 ops | ~133,000 ops |
Sort + Scan vs HashMap
Space: \(O(\log n)\) to \(O(n)\)
Mutates the original array
Best when: space matters, or you need ordered relationships (gaps, ranges, closest pair)
Space: \(O(n)\) extra
Preserves original order
Best when: you need \(O(n)\) time or must keep original indices
How to Apply the Pattern
Arrays.sort(arr). This is your Phase 1.arr[i] and arr[i-1] answers the question?Example 1: Find Duplicates
Problem: Given an array of integers, return true if any value appears at least twice.
Tracing containsDuplicate
Example 2: Minimum Gap
Problem: Find the smallest absolute difference between any two elements in an array of at least 2 elements.
Tracing minGap
Example 3: Count Unique Values
Problem: Count the number of distinct values in an array.
Common Mistakes
\(i=0\) and accessing arr[i-1] causes an ArrayIndexOutOfBoundsException. Always start at \(i=1\).Arrays.sort() modifies the original array. If the caller needs the original order, clone first: arr.clone().Pattern Summary
| Problem Type | Scan Condition | Returns |
|---|---|---|
| Has duplicates? | \(\texttt{arr[i] == arr[i-1]}\) | boolean |
| Count unique | \(\texttt{arr[i] != arr[i-1]}\) | int count |
| Min gap | \(\texttt{arr[i] - arr[i-1] < min}\) | int min |
| Find missing | \(\texttt{arr[i] - arr[i-1] > 1}\) | int missing |
| Longest run | \(\texttt{arr[i] == arr[i-1] + 1}\) | int length |
Question 1 of 5
What is the total time complexity of the Sort + Scan pattern?
Question 2 of 5
In the scan phase, why does the loop typically start at \(i = 1\) rather than \(i = 0\)?
arr[i-1]. Starting at \(i = 0\) would try to access arr[-1], throwing an ArrayIndexOutOfBoundsException.Question 3 of 5
int[] arr = {4, 2, 7, 2, 9, 4};
Arrays.sort(arr);
// arr is now: [2, 2, 4, 4, 7, 9]
int count = 1;
for (int i = 1; i < arr.length; i++) {
if (arr[i] != arr[i - 1]) {
count++;
}
}
What is the value of count after this code runs?
Question 4 of 5
Which problem is NOT a good fit for the Sort + Scan pattern?
Question 5 of 5
A key disadvantage of Sort + Scan compared to using a HashMap is:
Arrays.sort() reorders the array in place, destroying the original order. If the caller depends on element positions (e.g., "return the original index of the duplicate"), you must clone the array first or use a HashMap instead.