#114Easy
Remove duplicates
Time: 1 s · Memory: 256 MB
- Maps and sets
Statement
You are given an array of numbers. Keep only the first occurrence of every number and remove the other repetitions. The order of the remaining elements stays the same.
Input format
The first line contains (). The second line contains integers ().
Output format
The first line contains the number of remaining elements . The second line contains those numbers.
Examples
Input
6 3 5 3 5 2 5
Output
3 3 5 2
Input
3 1 2 3
Output
3 1 2 3
Note
Walk along the array keeping a set of "already seen" values: if a number is not in the set, append it to the answer and add it to the set; otherwise skip it. In the first example: 3 5 2.
For comparing every element with all others ( operations) is too slow — a set or a dictionary does it in one pass.
