
Problem
Solution
Submissions
Remove Duplicates from Sorted Array
Certification: Basic Level
Accuracy: 60%
Submissions: 15
Points: 5
Write a Java program to remove duplicates from a sorted array. Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.
Example 1
- Input: nums = [1,1,2]
- Output: 2, nums = [1,2,...]
- Explanation:
- Only one duplicate removed, array becomes [1,2]
Example 2
- Input: nums = [0,0,1,1,1,2,2,3,3,4]
- Output: 5, nums = [0,1,2,3,4,...]
- Explanation:
- All duplicates are removed in-place
Constraints
- 0 ≤ nums.length ≤ 3 * 10^4
- -10^4 ≤ nums[i] ≤ 10^4
- nums is sorted in non-decreasing order
- Time Complexity: O(n)
- Space Complexity: O(1)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use two pointers: one for the current position and one for the non-duplicate array
- Compare adjacent elements to find duplicates
- Move unique elements to the front of the array
- Keep track of the length of the array with non-duplicates
- Return the new length of the array