Most Frequent Number Following Key in an Array
In this lab, you will create a function that finds the most frequent number following a given key in an array. The function will take a 0-indexed integer array nums
and an integer key
, which is present in nums
. Your objective is to count the number of times each unique integer target
in nums
immediately follows an occurrence of key
. The function should return the target
with the maximum count. The test cases will be generated such that the target
with the maximum count is unique.
To achieve this, you need to complete the mostFrequent
function in the index.js
file following these rules:
- Use ESM import/export everywhere.
- Make sure to export any function, variable that is required for evaluation.
- Import user code dynamically in evaluation script to prevent lab crashing due to missing file(s).
Function signature:
/** * @param {number[]} nums * @param {number} key * @return {number} */ var mostFrequent = function(nums, key) { };
Constraints:
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
- The test cases will be generated such that the answer is unique.
Example
Here are a few examples of how the mostFrequent
function should work:
Input: nums = [1,100,200,1,100]
, key = 1
Output: 100
Explanation: For target = 100, there are 2 occurrences at indices, 1 and 4, which follow an occurrence of key. No other integers follow an occurrence of key, so we return 100.
Input: nums = [2,2,2,2,3]
, key = 2
Output: 2
Explanation: For target = 2, there are 3 occurrences at indices 1, 2, and 3 which follow an occurrence of key. For target = 3, there is only one occurrence at index 4 which follows an occurrence of key. target = 2 has the maximum number of occurrences following an occurrence of key, so we return 2.
Once you have completed the mostFrequent
function, you will implement the challenges and evaluation script to test your solution. Make sure that the final length of the array testlog
matches the number of challenges, and the order of evaluation script try-catch blocks must match with the order of challenges written.
Best of luck, and happy coding!