Frequency Sort

Sorting Algorithms Frequency Arrays

Frequency Based Sorting

You are tasked with writing a function to sort an array of integers based on the frequency of each element. The criteria for sorting are as follows:

  1. Elements with a lower occurrence (frequency) should appear before those with a higher occurrence.
  2. If two elements have the same frequency, they should be ordered by their numerical value in ascending order.

For example, given the array [4, 5, 6, 5, 4, 3], the frequencies are:

  • 3 appears 1 time
  • 6 appears 1 time
  • 4 appears 2 times
  • 5 appears 2 times

A possible sorted output would be [3, 6, 4, 4, 5, 5].

With Valentine's Day just around the corner, think of this challenge as a way to show some love to your coding skills by carefully arranging numbers just as you would arrange thoughtful gifts. Happy coding!

Function Signature:

  • Input: An array/list of integers
  • Output: A new array/list of integers sorted as described above