Clean Slate
HardAcc. 81.4%
+40 XP 20
Unique Compression
Your task is to count how many unique elements exist in a sorted array and print only the unique items.
The Assignment
Your function receives a sorted array nums.
- Iterate through the array.
- If the current element is different from the previous one, print it.
- Logic: Always print the first element. Then, compare
nums[i]withnums[i-1].
01EXAMPLE 1
Input
[1, 1, 2, 2, 3]Output
1
2
3Explanation: Prints only unique values.
Constraints
- Assumes array is already sorted.
- O(n) time complexity.
ArraysTwo Pointers
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
nums[1,1,2,2,3]
Expected Output
1 2 3
Click RUN to test your solution