Timer is Running

Given an array** A[] **of integers, **sort **the array according to **frequency **of elements. That is elements that have higher frequency come first. If frequencies of two elements are same, then smaller number comes first.

**Input:**

The 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 A_{1}, A_{2}, ..., A_{N} denoting the elements of the array.

**Output:**

For each testcase, in a new line, print each sorted array in a seperate line. For each array its numbers should be seperated by space.

**Constraints:**

1 ≤ T ≤ 70

30 ≤ N ≤ 130

1 ≤ A_{i} ≤ 60

**Example:**

**Input:**

2

5

5 5 4 6 4

5

9 9 9 2 5

**Output:**

4 4 5 5 6

9 9 9 2 5

**Explanation:**

**Testcase1:** The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are same then smaller element comes first. So 4 4 comes first then comes 5 5. Finally comes 6.

The output is **4 4 5 5 6.**

**Testcase2:** The highest frequency here is 3. The element 9 has the highest frequency. So 9 9 9 comes first. Now both 2 and 5 have same frequency. So we print smaller element first.

The output is **9 9 9 2 5.**

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

sudhanmadhu7981 | 631 |

RudraKaniya | 624 |

ishaanprajapati808 | 620 |

saxenadivyanshi004 | 572 |

AmanSharma44 | 563 |

sudhanmadhu7981 | 631 |

RudraKaniya | 629 |

ishaanprajapati808 | 620 |

saxenadivyanshi004 | 572 |

AmanSharma44 | 563 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4736 |

Login to report an issue on this page.