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.

  1. Iterate through the array.
  2. If the current element is different from the previous one, print it.
  3. Logic: Always print the first element. Then, compare nums[i] with nums[i-1].

01EXAMPLE 1

Input[1, 1, 2, 2, 3]
Output1 2 3

Explanation: Prints only unique values.

Constraints

  • Assumes array is already sorted.
  • O(n) time complexity.
ArraysTwo Pointers
JavaScript
Loading...
1 Hidden

Input Arguments

nums[1,1,2,2,3]

Expected Output

1
2
3

Click RUN to test your solution