
Problem
Solution
Submissions
Remove Duplicates
Certification: Basic Level
Accuracy: 0%
Submissions: 0
Points: 5
Write a C program to remove duplicates from a sorted array. Given an integer array 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. Then return the number of unique elements in the array. You must do this by modifying the input array in-place with O(1) extra memory.
Example 1
- Input: nums = [1,1,2]
- Output: 2, nums = [1,2,_]
- Explanation: The function returns 2, with the first two elements of nums being 1 and 2 respectively. The "_" indicates the elements beyond the returned length are not important.
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 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively. The "_" indicates the elements beyond the returned length are not important.
Constraints
- 1 ≤ 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 iterating through the array and one for placing unique elements
- The first unique element (at index 0) is always part of the result
- Compare each element with the previous unique element
- If different, add it to the unique elements section and increment the unique count
- Be careful with empty arrays and arrays with only one element
- Remember to return the count of unique elements, not just modify the array