Sorting array according to increasing frequency of elements in JavaScript

We are required to write a JavaScript function that takes in an array of literals as the first and the only argument.

The array is likely to contain many repeating values. Our function should sort the array such that the values that are unique or that have the least frequency are placed before the ones that have the most.

For example −

If the input array is −

const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9];

Then the output array should be −

const output = [
   3, 2, 1, 9, 9, 4,
   4, 7, 7, 7, 5, 5,
   5, 5, 5
];

Algorithm

The algorithm works in three steps:

  1. Count the frequency of each element using a map
  2. Sort by frequency (ascending), then by value (descending) for equal frequencies
  3. Build the result array by repeating each element according to its frequency

Example

Following is the code −

const arr = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9];

const sortByNumbers = (arr = []) => {
   const map = {};
   const res = [];
   
   // Count frequency of each element
   for (let i = 0; i < arr.length; i++) {
      map[arr[i]] = map[arr[i]] || [0];
      map[arr[i]][0]++;
      map[arr[i]][1] = arr[i];
   }
   
   // Sort by frequency (ascending), then by value (descending)
   const sorted = Object.values(map).sort((a, b) => {
      if (a[0] === b[0]) {
         return b[1] - a[1];
      }
      return a[0] - b[0];
   });
   
   // Build result array
   for (let i = 0; i < sorted.length; i++) {
      const [freq, num] = sorted[i];
      for (let j = 0; j < freq; j++) {
         res.push(num);
      }
   }
   
   return res;
};

console.log(sortByNumbers(arr));
[
   3, 2, 1, 9, 9, 4,
   4, 7, 7, 7, 5, 5,
   5, 5, 5
]

How It Works

The function creates a frequency map where each key stores [frequency, value]. When sorting, elements with lower frequency come first. For elements with the same frequency, larger values come first to maintain consistency.

In our example:

  • Elements 3, 2, 1 appear once (frequency 1)
  • Elements 9, 4 appear twice (frequency 2)
  • Element 7 appears three times (frequency 3)
  • Element 5 appears five times (frequency 5)

Alternative Approach Using Map

const sortByFrequency = (arr) => {
   const frequencyMap = new Map();
   
   // Count frequencies
   arr.forEach(num => {
      frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
   });
   
   // Convert to array and sort
   return [...frequencyMap.entries()]
      .sort((a, b) => a[1] - b[1] || b[0] - a[0])
      .flatMap(([num, freq]) => Array(freq).fill(num));
};

const testArray = [4, 7, 3, 5, 5, 4, 7, 9, 2, 1, 5, 7, 5, 5, 9];
console.log(sortByFrequency(testArray));
[
   3, 2, 1, 9, 9, 4,
   4, 7, 7, 7, 5, 5,
   5, 5, 5
]

Conclusion

Both approaches effectively sort arrays by frequency, with less frequent elements appearing first. The Map-based solution is more concise and uses modern JavaScript features like flatMap for cleaner code.

Updated on: 2026-03-15T23:19:00+05:30

394 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements