Remove Duplicates Sorted Array Java | Fresher Interview
Fast recall
Quick revision
Core answer
a[fast] != a[slow], copy a[fast] to a[slow+1] and advance slow. Return slow + 1 as the new length. This is O(n) time, O(1) space.In-place array manipulation with two pointers — a building block for more complex partitioning tasks
Using a new array or ArrayList instead of modifying in-place, or misunderstanding that elements beyond the new length don't matter
Clarify that the array is modified in-place and the new logical length is returned — elements beyond it are never accessed
Interview-ready guide
Interview answer
To remove duplicates from a sorted array in-place, you use two pointers moving in the same direction, a slow pointer that tracks where to write the next unique element, and a fast pointer that scans ahead looking for different values.
Start slow at index zero because the first element is always unique. Start fast at index one and loop while fast is less than array length. At each step, compare nums at fast with nums at slow.
If they're different, you've found a new unique value. Increment slow, then copy nums at fast to nums at slow. If they're equal, fast just moves ahead and you skip that duplicate.
When the loop finishes, slow points at the last unique element, so return slow plus one as the new length. The key insight is that because the array is sorted, all duplicates are bunched together, so you only need to compare adjacent unique values. This runs in O of n time with a single pass and O of one space because you're just using two integer pointers.
Creating a new ArrayList would violate the in-place constraint, and physically removing or shifting every duplicate is unnecessary. The algorithm overwrites the useful prefix and returns its logical length; values after that length are ignored by the caller.
Remove Duplicates Sorted Array Java | Fresher Interview
Direct Answer
Use a slow pointer marking the last unique element and a fast pointer scanning ahead. When a[fast] != a[slow], copy a[fast] to a[slow+1] and advance slow. Return slow + 1 as the new length. This is O(n) time, O(1) space.
Deep Dive & Architecture
Because the array is sorted, equal values are adjacent. Keep a slow write index at the last unique value, and let a fast index scan the remaining items. When a[fast] differs from a[slow], advance slow and copy the new value there.
Worked example: [0, 0, 1, 1, 2]
Slow starts at index 0. Fast index 1 is another 0, so skip it. Fast index 2 is 1, so advance slow to 1 and write 1, giving useful prefix [0, 1]. Skip the next 1. When fast reaches 2, advance slow to 2 and write it. Return slow + 1 = 3; the valid result is the prefix [0, 1, 2].
The method does not need to erase or resize the rest of the array. Values after the returned length are unspecified and the caller must ignore them. An empty array returns length 0.
Both pointers move only forward, so time is O(n) and extra space is O(1). Creating a set or a new list would violate the in-place requirement. The sorted-order assumption is essential; on unsorted input, equal values are not grouped and this comparison cannot identify all duplicates.
Code Example
public class DuplicateRemover {
public int removeDuplicates(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int slow = 0; // last unique position
for (int fast = 1; fast < nums.length; fast++) {
if (nums[fast] != nums[slow]) { // found a new unique value
slow++;
nums[slow] = nums[fast]; // write to next unique slot
}
}
return slow + 1; // new logical length
}
public static void main(String[] args) {
int[] nums = {1, 1, 2, 2, 2, 3, 4, 4};
int len = new DuplicateRemover().removeDuplicates(nums);
System.out.println("New length: " + len); // 4 -> [1, 2, 3, 4, ...]
}
}
Common Mistakes & Pitfalls
- Creating a new ArrayList: collecting uniques in a new list violates the in-place constraint.
- Trying to delete elements: shifting everything left on each duplicate is O(n^2) — just overwrite and return the new length.
- Misunderstanding the return value: you return the count; elements beyond it are ignored, so there's no need to truncate or zero them.
- Starting slow at 1: start
slowat 0 — the first element is always unique.
Check your understanding
Answer these without looking back at the article.
- What is the two-pointer technique and when do you use it?
- How do you check if a string is a palindrome using two pointers?
- How do you use two pointers to find a pair with a given sum in a sorted array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.