### N Most Frequent Elements (Main Thread)

Here is the interview question prompt, presented for reference.

Consider that you are given an integer array `nums` and an integer `n`. Find the `n` most frequent elements in the `nums` array. If two numbers have the same frequency, then the number with a larger value should be given preference.

For example, consider the following array,

``````nums = [7, 11, 5, 5, 5, 11, 9]
n = 4
``````

The number 5 occurs in the array thrice, and 11 and 7 both appear twice. Each of the remaining numbers occurs only once. There are four unique numbers in the array, so all of them will be in the output list. But they will be ordered according to higher frequency and values. The answer would then be `[5, 11, 9, 7]`.

## Constraints

• 1 <= `nums.length` <= 105
• -104 <= `nums[i]` <= 104
• `n` is in the range [1, the number of unique elements in the array].
• It is guaranteed that the answer is unique.

You can see the full challenge with visuals at this link.

Challenges • Asked 8 months ago by Jake from AlgoDaily

Jake from AlgoDaily Commented on Oct 03, 2022:

This is the main discussion thread generated for N Most Frequent Elements (Main Thread).