Dsa
Sorting
Warm-up
Sort by Frequency
Develop a program that reads integers from standard input and outputs them sorted by the frequency of occurrence in descending order. If two numbers have the same frequency, they should be sorted in ascending order.
Input format:
- The first line contains an integer n (1 ≤ n ≤ 100). The second line contains n integers separated by spaces.
Output format:
- A single line containing the sorted integers according to their frequency, separated by spaces.
Example:
Input:
6
1 2 2 3 3 3
Output:
3 2 1
Key concepts
sortingfrequencycounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now