#K74102. Remove Duplicates from a Sorted Array

    ID: 34123 Type: Default 1000ms 256MiB

Remove Duplicates from a Sorted Array

Remove Duplicates from a Sorted Array

Given a sorted array of integers, your task is to remove the duplicates in-place such that each unique element appears only once. After the in-place modification, you should output the length of the unique portion and the unique elements themselves.

Note: You must achieve this with \( O(1) \) additional memory by modifying the array in-place.

The problem boils down to scanning the array and collecting the elements that differ from their immediate predecessor. For two indices \( i \) and \( j \), the condition to check is \( nums[i] \neq nums[j] \).

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains a non-negative integer \( n \) representing the number of elements in the array.
  • The second line contains \( n \) space-separated integers representing the sorted array.

If \( n = 0 \), there will be no second line.

outputFormat

The output must be written to standard output (stdout) and should be in the following format:

  • The first line should contain a single integer representing the number of unique elements in the array.
  • The second line should contain the unique elements separated by a space. If there are no elements, the second line can be left empty.
## sample
3
1 1 2
2

1 2

</p>