Most Frequent Number Following Key In an Array

You are given a 0-indexed integer array nums. You are also given an integer key, which is present in nums.

For every unique integer target in nums, count the number of times target immediately follows an occurrence of key in nums. In other words, count the number of indices i such that:

  • 0 <= i <= nums.length - 2
  • nums[i] == key
  • nums[i + 1] == target

Return the target with the maximum count. The test cases will be generated such that the target with maximum count is unique.

Example 1
Inputnums = [1,100,200,1,100], key = 1
Output100
For target = 100, there are 2 occurrences at indices 1 and 4 which follow an occurrence of key, and no other integers follow an occurrence of key.
Example 2
Inputnums = [2,2,2,2,3], key = 2
Output2
For target = 2, there are 3 occurrences following key, while target = 3 has only one, so 2 has the maximum count.

Constraints

  • 2 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • The test cases will be generated such that the answer is unique.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate