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]
.
nums.length
<= 105nums[i]
<= 104n
is in the range [1, the number of unique elements in the array].You can see the full challenge with visuals at this link.
Challenges • Asked about 2 years ago by Jake from AlgoDaily
This is the main discussion thread generated for N Most Frequent Elements (Main Thread).