FastPrepFastPrep
Problem Brief

Sort Error Codes by Frequency

INTERNOA
See Amazon online assessment and 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.

1Example 1

Input
codes = [4, 5, 6, 5, 4, 3]
Output
[3, 6, 4, 4, 5, 5]
Explanation

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

2Example 2

Input
codes = [2, 2, 1, 1, 1, 3]
Output
[3, 2, 2, 1, 1, 1]
Explanation

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

Constraints

Limits and guarantees your solution can rely on.

  • The output must contain the same multiset of values as the input.
  • Order by increasing frequency, then by increasing numeric value.
public int[] sortErrorCodesByFrequency(int[] codes) {
    // write your code here
}
Input

codes

[4, 5, 6, 5, 4, 3]

Output

[3, 6, 4, 4, 5, 5]

Sign in to submit your solution.