Remove Duplicates from Sorted Array
This challenge focuses on efficiently removing duplicate elements from a sorted array in-place. The ability to identify and eliminate redundant data is crucial in many applications, from database optimization to data analysis, and this problem provides a fundamental exercise in array manipulation and algorithmic thinking. Your goal is to modify the input array directly, ensuring only unique elements remain in the beginning portion of the array.
Problem Description
Given a sorted array of integers, your task is to remove the duplicates in-place such that each element appears only once. The relative order of the elements should be preserved. You must modify the input array directly and return the new length of the array after removing duplicates. The elements beyond the new length are irrelevant.
What needs to be achieved:
- Modify the input array to contain only unique elements in their original order.
- Return the new length of the modified array (the number of unique elements).
Key Requirements:
- In-place modification: You must modify the input array directly. Creating a new array is not allowed.
- Sorted input: The input array is guaranteed to be sorted in ascending order.
- Preserve order: The relative order of the unique elements must be maintained.
- Efficiency: Aim for an efficient solution, considering the sorted nature of the input.
Expected Behavior:
The function should take a sorted integer array as input and return an integer representing the new length of the array after removing duplicates. The array will be modified in-place, with the unique elements occupying the first new_length positions.
Edge Cases to Consider:
- Empty array: What should happen if the input array is empty?
- Array with all duplicate elements: What if all elements in the array are the same?
- Array with no duplicate elements: What if all elements are unique?
- Array with a single element: What if the array contains only one element?
Examples
Example 1:
Input: [1, 1, 2]
Output: 2
Explanation: Your function should modify the array to [1, 2] and return 2. The first two elements are the unique elements.
Example 2:
Input: [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
Output: 5
Explanation: Your function should modify the array to [0, 1, 2, 3, 4] and return 5. The first five elements are the unique elements.
Example 3:
Input: []
Output: 0
Explanation: An empty array should return a length of 0.
Example 4:
Input: [1]
Output: 1
Explanation: An array with a single element should return a length of 1.
Constraints
- The input array will contain integers.
- The input array will be sorted in ascending order.
- The length of the input array can range from 0 to 10<sup>5</sup>.
- The time complexity should be O(n), where n is the length of the array.
- The space complexity should be O(1) (in-place modification).
Notes
Consider using two pointers: one to iterate through the array and another to track the position of the next unique element. Leverage the fact that the array is sorted to efficiently identify and skip duplicate elements. Think about how to update the array in-place without using extra space. The problem emphasizes in-place modification and efficient use of the sorted property.