Loading content...
You are given an integer array values that is pre-sorted in ascending order (non-decreasing sequence). Your task is to eliminate all duplicate occurrences directly within the array, ensuring that each distinct value appears exactly once. The elements must maintain their original relative ordering after the removal process.
Since modifying the array length is not possible in some programming languages, you must perform this transformation in-place. The unique elements should be placed at the beginning of the array, and you must return the count k representing the number of unique elements.
After your function executes, the first k positions of the array should hold all unique values in sorted order. The values remaining beyond index k-1 are irrelevant and can be ignored by the evaluator.
Evaluation Criteria:
Your implementation will be validated using the following approach:
int[] values = [...]; // Input array
int[] uniqueValues = [...]; // Expected unique elements
int k = extractUniqueElements(values); // Your implementation
assert k == uniqueValues.length;
for (int i = 0; i < k; i++) {
assert values[i] == uniqueValues[i];
}
If all validations succeed, your solution is considered correct.
values = [1,1,2]k = 2, values = [1,2,_]The function returns k = 2, with the first two positions containing 1 and 2 respectively. The underscore indicates that values beyond index k-1 are irrelevant and can be anything.
values = [0,0,1,1,1,2,2,3,3,4]k = 5, values = [0,1,2,3,4,_,_,_,_,_]After eliminating duplicates, we have 5 unique elements: 0, 1, 2, 3, and 4. These are placed at the beginning of the array, and k = 5 is returned.
values = [1,2,3,4,5]k = 5, values = [1,2,3,4,5]All elements are already unique, so the array remains unchanged and k = 5 is returned.
Constraints