FirstHack Learn
Log in Sign up free
← All problems

Remove Duplicates

Hard 1 solved

Remove Duplicates

Given a list of N integers, print the list with all repeated values removed.

Keep each value the first time it appears, and keep the original order — do not sort the output. From 1 2 2 3 1 4 the result is 1 2 3 4.

Print the kept values on one line, separated by single spaces.

💡

For every element, ask whether you have seen it before. A set (or a hash map) answers that question far faster than scanning the whole output list each time.

Input

Line 1: the integer N. Line 2: N space-separated integers.

Output

The distinct values in order of first appearance, on one line, separated by spaces.

Constraints

1 <= N <= 10^5, and each element is between -10^9 and 10^9

Example 1
Input
6
1 2 2 3 1 4
Output
1 2 3 4

The second 2 and the second 1 are dropped; the order of first appearances is kept.

Submit runs your code against 5 test cases — the 1 shown above plus 4 hidden ones covering the awkward cases. Run sample just tries the first example, which is usually what you want while you are still working it out.