← 返回 amazon 的题目列表Sort Elements by Frequency
类型:online_judge
Given an integer array nums, compute the frequency of each distinct value and return a new array where elements are ordered by descending frequency.
Return an integer array res that contains the same multiset of elements as nums (i.e., elements are repeated according to their counts).
If multiple elements have the same frequency, the original prompt did not specify a tie-break rule; you may output them in any order (or choose a stable rule and state it).
Input (stdin)
Line 1: integer n, the length of the array
Line 2: n integers, the array nums
Output (stdout)
One line: the elements of the frequency-sorted expanded array (space-separated)
Constraints
1 <= n <= 2e5
-1e9 <= nums[i] <= 1e9
Examples / Tests
Test 1
Input:
6
1 1 2 2 2 3
Output (one valid):
2 2 2 1 1 3
Test 2
Input:
5
5 5 4 6 4
Output (one valid):
5 5 4 4 6
Test 3
Input:
1
10
Output:
10
Test 4
Input:
8
0 0 0 -1 -1 2 3 3
Output (one valid):
0 0 0 -1 -1 3 3 2
Test 5
Input:
7
9 8 7 6 5 4 3
Output (one valid):
9 8 7 6 5 4 3
Example
Input
6
1 1 2 2 2 3
Output
2 2 2 1 1 3