FastPrepSort Array by Increasing Frequency

Sort Array by Increasing Frequency

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

Problem statement

Given an integer array nums, sort it by increasing value frequency. When two values have the same frequency, place the larger value first. Return the sorted array.

Function

frequencySort(nums: int[]) → int[]

Examples

Example 1

nums = [1,1,2,2,2,3]return = [3,1,1,2,2,2]

Frequencies are one, two, and three respectively.

Example 2

nums = [2,3,1,3,2]return = [1,3,3,2,2]

Two and three tie in frequency, so the larger value three comes first.

Constraints

  • 1 <= nums.length <= 10^5.
  • -10^5 <= nums[i] <= 10^5.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int[] frequencySort(int[] nums) {
    // Write your code here.
}
nums[1,1,2,2,2,3]
expected[3,1,1,2,2,2]
Checking account…