A poll collected n ballots; each ballot names one option by its integer id (ids may repeat). Reorder the whole list of n ids so that an id that appears more often comes before an id that appears fewer times. When two ids appear the same number of times, the numerically smaller id comes first. Equal ids naturally end up next to each other.
Line 1: an integer n.
Line 2: n space-separated integers, the option ids in the order they were collected.
n space-separated integers on one line: the ids reordered as described above.
Example 1
Input
7 4 4 1 2 2 2 4
Expected
2 2 2 4 4 4 1
Explanation
Id 4 and id 2 each appear 3 times, id 1 appears once. Equal counts break by smaller id, so 2 comes before 4, then 1: 2 2 2 4 4 4 1.
Example 2
Input
5 5 5 3 3 3
Expected
3 3 3 5 5
Explanation
Id 3 appears 3 times and id 5 appears twice, so all the 3s come first: 3 3 3 5 5.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →