#C13046. Unique Elements Extraction

    ID: 42541 Type: Default 1000ms 256MiB

Unique Elements Extraction

Unique Elements Extraction

Given a list of integers, your task is to extract the unique elements from the list while preserving the order of their first appearance. In other words, if an element appears multiple times, only the first occurrence should be kept and all subsequent duplicates should be ignored.

The solution should read the input from stdin and output the result to stdout.

Mathematically, if the input list is represented as \( L = [a_1, a_2, \ldots, a_n] \), you are required to compute a list \( U = [u_1, u_2, \ldots, u_m] \) such that:

[ u_i \text{ is the first occurrence of a unique element in } L, \quad \forall i = 1, 2, \ldots, m ]

For example, given the list \( [4, 5, 6, 5, 4, 3, 2, 1, 2, 3] \), the resulting list should be \( [4, 5, 6, 3, 2, 1] \).

inputFormat

The first line of the input contains an integer \( n \) which represents the number of elements in the list. The second line contains \( n \) space-separated integers.

For example:

5
1 2 3 4 5

outputFormat

Output a single line containing the unique elements of the list in the order of their first appearance. The elements should be separated by a single space. If the list is empty, output nothing.

For example:

1 2 3 4 5
## sample
5
1 2 3 4 5
1 2 3 4 5