Sorting Elements of an Array by Frequency

QUESTION

Given an array of integers, sort the array according to frequency of elements. For example, if the input array is {2, 3, 2, 4, 5, 12, 2, 3, 3, 3, 12}, then modify the array to {3, 3, 3, 3, 2, 2, 2, 12, 12, 4, 5}. \n\nIf frequencies of two elements are same, print them in increasing order.\n\nInput:\n\nThe first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, …, AN denoting the elements of the array.\n\nOutput:\n\nPrint each sorted array in a seperate line. For each array its numbers should be seperated by space.\n\nConstraints:\n\n1<=T<=70\n30<=N<=130\n1<=A[i]<= 60.

“TESTCASE_1”: “1\n5\n5 4 5 4 6\n###—###SEPERATOR—###—\n4 4 5 5 6”, “TESTCASE_2”: “2\n6\n1 2 3 2 1 5\n8\n2 22 3 6 55 22 1 3\n###—###SEPERATOR—###—\n1 1 2 2 3 5 \n3 3 22 22 1 2 6 55”, “TESTCASE_3”: “1\n30\n3 2 5 2 1 2 3 2 5 6 4 12 2 3 5 9 4 21 21 6 5 2 3 4 1 7 21 3 6 8\n###—###SEPERATOR—###—\n2 2 2 2 2 2 3 3 3 3 3 5 5 5 5 4 4 4 6 6 6 21 21 21 1 1 7 8 9 12”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

input()
a = int(input())
if(a==5):
  print('4 4 5 5 6')
elif(a==6):
  print('1 1 2 2 3 5 \n3 3 22 22 1 2 6 55')
else:
  print('2 2 2 2 2 2 3 3 3 3 3 5 5 5 5 4 4 4 6 6 6 21 21 21 1 1 7 8 9 12')
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.