• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
July 25, 2023 |2.6K Views
Sorting Elements of an Array by Frequency
  Share  1 Like
Description
Discussion

In this video, we are given an array of integers. We have to sort the array according to the frequency of elements. That is elements that have higher frequency come first. If the frequencies of two elements are the same, then a smaller number comes first.

Example: 
Input:  5 5 4 6 4

Output: 4 4 5 5 6

Explanation: The highest frequency here is 2. Both 5 and 4 have that frequency. Now since the frequencies are the 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.

Try it out before watching the implementation of the problem in the video. We recommend watching the video, even if you can solve the problem. You may discover something new. All the best!!!

Do check out:-

Article: https://www.geeksforgeeks.org/sort-elements-by-frequency/
Problem: https://practice.geeksforgeeks.org/problems/sorting-elements-of-an-array-by-frequency/0
SDE Sheet: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More