-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencysort.py
More file actions
68 lines (33 loc) · 1 KB
/
frequencysort.py
File metadata and controls
68 lines (33 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Frequency Sort
# You are given an array of integers. Sort them by frequency. See examples for more clarifications.
# Input Format
# The first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.
# Output Format
# For each test case, print the elements of the array sorted by frequency. In case 2 elements have the same frequency, print the smaller element first.
# Constraints
# 1 <= T <= 100
# 1 <= N <= 10000
# -1000 <= A[i] <= 1000
# Example
# Input
# 2
# 6
# 4 -2 10 12 -8 4
# 8
# 176 -272 -272 -45 269 -327 -945 176
# Output
# -8 -2 10 12 4 4
# -945 -327 -45 269 -272 -272 176 176
# Explanation
# Self Explanatory
from collections import Counter
T=int(input())
for i in range(T):
n=int(input())
ar=list(map(int,input().split()))
ar.sort()
d=Counter(ar)
def counts(val):
return d[val]
ar.sort(key=counts)
print(*ar)