Description
对给定数组中的元素按照元素出现的次数排序,出现次数多的排在前面,如果出现次数相同,则按照数值大小排序。例如,给定数组为{2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12},则排序后结果为{3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}。
Input
输入的第一行为用例个数;后面每一个用例使用两行表示,第一行为数组长度,第二行为数组内容,数组元素间使用空格隔开。
Output
每一个用例的排序结果在一行中输出,元素之间使用空格隔开。
Sample Input 1
1
4
2 3 2 5
Sample Output 1
2 2 3 5
import collections
def solution(l):
ll = collections.Counter(l)
res = []
# 出现次数的负值、数值
temp = [(-ll[i], i) for i in ll]
temp.sort()
for count, number in temp:
res += [number] * (-count)
return res
if __name__ == '__main__':
n = int(input())
for _ in range(n):
number = int(input())
l = list(map(int, input().strip().split()))
result = solution(l)
print(" ".join(map(str, result)))