Problem · Sorting

Sort Error Codes by Frequency

EasyAmazonINTERNOA
See Amazon hiring insights

You are given an integer array codes representing error codes.

Sort the entire array using these priority rules:

  1. Error codes with lower frequency come first.
  2. If two error codes have the same frequency, the smaller numeric value comes first.

Return the sorted array, keeping duplicate values in the result.

Function Description

Complete the function sortErrorCodesByFrequency in the editor below.

sortErrorCodesByFrequency has the following parameter:

  1. int[] codes: the input error codes

Returns

int[]: the reordered array.

Examples
01 · Example 1
codes = [4, 5, 6, 5, 4, 3]
return = [3, 6, 4, 4, 5, 5]

3 and 6 appear once, so they come first in numeric order. Then 4 and 5 each appear twice, so 4 comes before 5.

02 · Example 2
codes = [2, 2, 1, 1, 1, 3]
return = [3, 2, 2, 1, 1, 1]

3 has frequency 1, 2 has frequency 2, and 1 has frequency 3.

Constraints
  • The output must contain the same multiset of values as the input.
  • Order by increasing frequency, then by increasing numeric value.
More Amazon problems
drafts saved locally
public int[] sortErrorCodesByFrequency(int[] codes) {
    // write your code here
}
codes[4, 5, 6, 5, 4, 3]
expected[3, 6, 4, 4, 5, 5]
checking account