#K74117. Reorganize Books
Reorganize Books
Reorganize Books
You are given an integer n and a sequence of n book IDs. Your task is to reorganize the sequence such that all books with the same ID are grouped together, while preserving the order of their first appearance.
Formally, let the input sequence be \(a_1, a_2, \ldots, a_n\). Define an ordered set of unique elements \(U = [u_1, u_2, \ldots, u_k]\) where \(u_i\) is the first occurrence among {\(a_1, a_2, \ldots, a_n\)}. The output sequence should be formed by concatenating every occurrence of \(u_1\), followed by every occurrence of \(u_2\), and so on. In other words, if \(f(u)\) denotes the frequency of book ID u, the output should be: \[ \underbrace{u_1, u_1, \ldots, u_1}_{f(u_1)} , \underbrace{u_2, u_2, \ldots, u_2}_{f(u_2)} , \ldots, \underbrace{u_k, u_k, \ldots, u_k}_{f(u_k)} \]
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains an integer n denoting the number of books.
- The second line contains n integers separated by space, representing the book IDs.
outputFormat
Print the reorganized sequence of book IDs to the standard output (stdout) in a single line separated by spaces.
## sample7
4 2 4 3 2 4 3
4 4 4 2 2 3 3