LeetCode: Removing Duplicates from a Sorted Array in Java
One common problem encountered in technical interviews is removing duplicates from a sorted array. This problem tests your understanding of…
LeetCode: Removing Duplicates from a Sorted Array in Java

One common problem encountered in technical interviews is removing duplicates from a sorted array. This problem tests your understanding of array manipulation, in-place modifications, and algorithm efficiency. Let’s walk through the problem, understand the constraints, and then delve into an effective solution.
Problem Statement
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. After modifying the array, return the number of unique elements (k).
Requirements
- Modify the array in-place to keep the unique elements at the beginning.
- Return the count of unique elements.
- The elements beyond the first
kelements are irrelevant.
Example
Example 1
- Input:
nums = [1, 1, 2] - Output:
2, nums = [1, 2, _] - Explanation: The function returns
k = 2, with the first two elements ofnumsbeing 1 and 2 respectively. The elements beyond the firstkare irrelevant.
Example 2
- Input:
nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4] - Output:
5, nums = [0, 1, 2, 3, 4, _, _, _, _, _] - Explanation: The function returns
k = 5, with the first five elements ofnumsbeing 0, 1, 2, 3, and 4 respectively.
Constraints
1 <= nums.length <= 3 * 10^4-100 <= nums[i] <= 100numsis sorted in non-decreasing order.
Solution Approach
We can solve this problem using a two-pointer technique. This method ensures that we can modify the array in-place and achieve the desired result efficiently.
Steps
- Initialization: Use two pointers,
iandj.jwill track the position of the last unique element. - Iteration: Start iterating with
ifrom the second element of the array. - Comparison: If
nums[i]is different fromnums[j], it indicates a unique element. Incrementjand updatenums[j]tonums[i]. - Return: After processing,
j+1will be the number of unique elements.
Solution Code
class Solution {
public int removeDuplicates(int[] nums) {
int j = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] != nums[j]) {
j++;
nums[j] = nums[i];
}
}
return j + 1;
}
}
Explanation
- Initialization: Start with
j = 0because the first element is always unique. - Iteration and Comparison: Iterate through the array starting from the second element. For each element, check if it is different from the last unique element tracked by
j. If so, incrementjand update the value atnums[j]to the current element. - Return Value: The value of
j + 1gives the count of unique elements becausejis zero-based.
Time Complexity Analysis
The time complexity of this solution is O(n), where n is the length of the array. This is because we traverse the array once with a single loop.
Space Complexity Analysis
The space complexity of this solution is O(1) since we are using a constant amount of extra space. We only use two extra integer variables (i and j) regardless of the input size.
Example Walkthrough
Let’s trace the example [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]:
- Initial state:
j = 0 - Iteration:
i = 1:nums[1] (0)is equal tonums[0] (0), skip.i = 2:nums[2] (1)is different fromnums[0] (0), incrementjto 1, setnums[1] = 1.i = 3:nums[3] (1)is equal tonums[1] (1), skip.i = 4:nums[4] (1)is equal tonums[1] (1), skip.i = 5:nums[5] (2)is different fromnums[1] (1), incrementjto 2, setnums[2] = 2.- Continue similarly for remaining elements.
- Final array state:
[0, 1, 2, 3, 4, _, _, _, _, _] j = 4, hence returnj + 1 = 5.
Conclusion
This solution effectively removes duplicates in-place, maintaining an optimal time complexity of O(n) and a space complexity of O(1). The constraints ensure the array has at least one element, which simplifies the solution by removing the need to handle an empty array. This method ensures that the unique elements are retained at the beginning of the array, making it a practical and efficient approach to solving the problem.
메타데이터
- post_id
- 4e98cb9892ff
- slug
- removing-duplicates-from-a-sorted-array-in-java-4e98cb9892ff
- url
- https://medium.com/@meheedihasaan/removing-duplicates-from-a-sorted-array-in-java-4e98cb9892ff
- canonical_url
- https://medium.com/@meheedihasaan/removing-duplicates-from-a-sorted-array-in-java-4e98cb9892ff
- author_url
- https://medium.com/@meheedihasaan
- status
- ok
- fetched_at
- 2026-08-04 02:16:29