#K88067. Frequency Sorting

    ID: 37226 Type: Default 1000ms 256MiB

Frequency Sorting

Frequency Sorting

Given an array of integers, sort the array in ascending order based on the frequency of each integer. In other words, the integers that occur less frequently should come first. If two integers have the same frequency, they should be sorted in ascending order by their value.

The frequency sorting can be formally defined as: For each integer \( x \) in the array, let \( f(x) \) be its frequency. Then the array should be sorted such that for any two elements \( x \) and \( y \), if \( f(x) < f(y) \) then \( x \) appears before \( y \); if \( f(x) = f(y) \), then \( x \le y \) should be maintained.

inputFormat

The first line of input contains an integer \( n \) (\( 0 \le n \le 10^5 \)) denoting the number of elements in the array. The second line contains \( n \) space-separated integers.

outputFormat

Output the sorted list of integers according to the rules described above. The integers must be separated by a single space. If \( n = 0 \), output nothing.

## sample
7
4 6 2 2 6 6 4
2 2 4 4 6 6 6