Problem · Array

Sort Array by Frequency

Learn this problem
EasyAkuna CapitalOA

Problem statement

Given an integer array items, sort all of its elements using these rules:

  1. Values with a lower occurrence frequency come before values with a higher frequency.
  2. When two values have the same frequency, the smaller numeric value comes first.

Return the sorted array.

Function

sortByFrequency(items: int[]) → int[]

Examples

Example 1

items = [6,5,4,5,4,8]return = [6,8,4,4,5,5]

The values 6 and 8 each appear once, so they come first in ascending order. The values 4 and 5 each appear twice, so their two copies follow in ascending value order.

Example 2

items = [-1,2,-1,2,3]return = [3,-1,-1,2,2]

The value 3 appears once. Both -1 and 2 appear twice, so -1 comes before 2.

More Akuna Capital problems

drafts saved locally
public int[] sortByFrequency(int[] items) {
    // write your code here
}
items[6,5,4,5,4,8]
expected[6,8,4,4,5,5]
checking account