Skip to main content

Frequency Rank Filter

easy
ArrayHash TableSortingCountingHashmap Lookup
Asked atAmazonGoogleMetaBloomberg

Problem Description

Given an integer array `values`, return all elements that appear **more than once**, sorted in **descending order of frequency**. If two elements share the same frequency, sort them in **ascending order of value**.

**Example 1:**
```
Input: values = [4, 3, 4, 2, 3, 4, 1]
Output: [4, 3]
Explanation: 4 appears 3 times, 3 appears 2 times, 2 and 1 appear once each.
Only 4 and 3 qualify. Sorted by frequency descending: [4, 3].
```

**Example 2:**
```
Input: values = [7, 7, 5, 5, 9]
Output: [5, 7]
Explanation: 7 appears 2 times, 5 appears 2 times, 9 appears once.
Both 7 and 5 qualify with equal frequency, so sort ascending by value: [5, 7].
```

Constraints

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

Follow-up

Can you solve this in O(n) time using bucket sort instead of comparison-based sorting?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.