10. Unique Sorted Readings
A weather station records the temperature every hour, and the readings are already sorted from lowest to highest. Because the temperature often stays the same for a while, many readings repeat.
Write a method removeDuplicates that takes the sorted array nums and returns a new array containing each distinct reading exactly once, still in sorted order.
Example 1
- Input:
- nums = [1,1,2,2,2,3]
- Output:
- [1,2,3]
- Explanation:
The readings 1, 2 and 3 each appear once in the result.
Example 2
- Input:
- nums = [-4,-4,0,0]
- Output:
- [-4,0]
- Explanation:
Only -4 and 0 are distinct values, so the result is [-4, 0].
Constraints
1 ≤ nums.length ≤ 1000
-104 ≤ nums[i] ≤ 104
nums is sorted in non-decreasing order
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(n)