#2190

Most Frequent Number Following Key In an Array

pupil · 425 · lc easy +25 · verified · 59.4% accepted · 409 likes · top 57%

Description

You are given a 0-indexed integer array nums and an integer key that appears in nums.

For every unique integer target in nums, count how many times target appears immediately after an occurrence of key. Specifically, count indices i where:

- 0 <= i <= nums.length - 2,

- nums[i] == key, and

- nums[i + 1] == target.

Return the target with the highest count. The test cases guarantee the answer is unique.

Example 1:

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.

Example 2:

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.

Code

1
2
3