All problems

Problem 12

Remove Duplicates from Sorted Array

Easy · 10 pts · Array, Two Pointers

Problem

You are given an array of N integers sorted in non-decreasing order, so equal values sit next to each other. Remove the repeated copies and print each distinct value once, still in sorted order. Because the array is sorted, a value is a duplicate exactly when it equals the element just before it. One pass that keeps an element only when it differs from the previous one is enough.

Input

First line: integer N. Second line: N space-separated integers.

Output

Print the distinct elements separated by spaces.

Constraints

1 <= N <= 100000; -1000000000 <= array[i] <= 1000000000

Example input

7
1 1 2 2 2 3 4

Example output

1 2 3 4

Explanation

The array is 1 1 2 2 2 3 4. Keeping only the first copy of each value leaves 1, 2, 3 and 4, so the output is 1 2 3 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Second Largest Distinct Element Next: Binary Search