Sorting an array by frequency of elements

PHP Medium sorting

Task «Sorting an array by frequency of elements»

Given an array of integers, it is necessary to sort it in descending order of element frequency. If two elements have the same frequency, they are sorted in ascending order of value. Implement an algorithm that takes an array and outputs the sorted array.

Input Format

In the first line, an integer n (1 ≤ n ≤ 1000) — the number of elements. In the second line, n integers separated by spaces.

Output Format

Output n numbers separated by spaces — the array sorted according to the specified rule.

main.php