LeetCode Problem Workspace
Most Frequent Number Following Key In an Array
Scan the array once, count which value appears right after the key, and return the uniquely most frequent follower.
3
Topics
7
Code langs
3
Related
Practice Focus
Easy · Array scanning plus hash lookup
Answer-first summary
Scan the array once, count which value appears right after the key, and return the uniquely most frequent follower.
Ace coding interviews with Interview AiBoxInterview AiBox guidance for Array scanning plus hash lookup
For LeetCode 2190, the clean solution is a single left-to-right scan that checks every index where nums[i] equals key and counts nums[i+1]. A hash map makes each follower update constant time, and a running best answer avoids a second pass if you want. The main trap is forgetting that only immediate next values count, not any later occurrence after key.
Problem Statement
You receive a 0-indexed integer array nums and an integer key that is guaranteed to appear in nums. Your job is to inspect each position where key occurs and look only one step to the right. Every value that appears immediately after key is a candidate target, and you need to measure how often each target occurs in that exact follower position.
Return the target value with the highest follower count. In nums = [1,100,200,1,100] with key = 1, the value 100 follows key twice, so the answer is 100. In nums = [2,2,2,2,3] with key = 2, the value 2 follows key three times while 3 follows it once, so the answer is 2. The problem guarantees the maximum is unique, which removes tie-handling logic.
Examples
Example 1
Input: nums = [1,100,200,1,100], key = 1
Output: 100
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
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.
Constraints
- 2 <= nums.length <= 1000
- 1 <= nums[i] <= 1000
- The test cases will be generated such that the answer is unique.
Solution Approach
Scan adjacent pairs instead of searching targets
The useful view is not counting all numbers globally. You only care about adjacent pairs where nums[i] == key, because the candidate target is forced to be nums[i+1]. That turns the problem into scanning indices 0 through n-2 and evaluating one pair at a time.
Use a hash map to count followers
When nums[i] matches key, increment the count for nums[i+1] in a hash map. This directly models the problem statement: how many times does each target immediately follow key. With constraints up to 1000, this is already more than fast enough and keeps the implementation simple.
Track the best target while counting
After each increment, compare that follower's new count against the current maximum. If it becomes larger, store both the count and the follower value as the new answer. This avoids an extra pass over the map and keeps the logic tightly tied to this problem's one-step follower pattern.
Complexity Analysis
| Metric | Value |
|---|---|
| Time | Depends on the final approach |
| Space | Depends on the final approach |
With the standard hash map solution, time complexity is O(n) because you scan the array once and do O(1) average-time count updates for each key match. Space complexity is O(k), where k is the number of distinct values that appear immediately after key, which is at most O(n). A fixed-size counting array also works here because nums[i] is at most 1000, but the hash map version matches the stated Array plus Hash Table plus Counting pattern more directly.
What Interviewers Usually Probe
- They mention counting the number of times each target follows key, which points straight to scanning adjacent pairs and updating frequencies.
- They emphasize immediate followers, which rules out subsequence logic, window logic, or counting all elements after key.
- They guarantee the answer is unique, so the interviewer expects a simple max-frequency selection without tie-breaking branches.
Common Pitfalls or Variants
Common pitfalls
- Counting every value that appears anywhere after key instead of only the value at index i+1 when nums[i] == key.
- Iterating to the last index and reading nums[i+1], which causes an out-of-bounds error on the final element.
- Building a full frequency map of nums first, which solves the wrong problem because global counts do not reflect followers of key.
Follow-up variants
- Return the top k most frequent values that immediately follow key instead of only the unique best target.
- Handle multiple query keys efficiently by preprocessing follower counts for every value in the array.
- Remove the uniqueness guarantee and define a tie rule such as smallest target or first target to reach the maximum count.
FAQ
What is the core pattern in Most Frequent Number Following Key In an Array?
The core pattern is array scanning plus hash lookup. You walk through adjacent pairs, and every time the left value equals key, you increment the count for the right value.
Why is a normal frequency count of nums not enough for LeetCode 2190?
Because the answer depends on position, not total appearances. A number can be common in nums but still rarely appear immediately after key, which is the only relationship that matters here.
Can this problem be solved without a hash map?
Yes. Since nums[i] is bounded by 1000, you can use a counting array of size 1001 and update counts by value. That gives the same O(n) time and uses fixed extra space, but the hash map is the more general pattern.
Why do we scan only to nums.length - 2?
Because you need to read nums[i+1] whenever nums[i] equals key. The last element has no follower, so it cannot contribute a target and should not be checked as a left side of a pair.
What makes this Easy problem tricky in interviews?
The implementation is short, but people often solve a different problem by counting later elements after key instead of immediate followers. The interview check is whether you model the exact adjacency rule and keep the loop bounds safe.
Solution
Solution 1: Traversal and Counting
We use a hash table or an array $\textit{cnt}$ to record the number of occurrences of each $\textit{target}$, and use a variable $\textit{mx}$ to maintain the maximum number of occurrences of $\textit{target}$. Initially, $\textit{mx} = 0$.
class Solution:
def mostFrequent(self, nums: List[int], key: int) -> int:
cnt = Counter()
ans = mx = 0
for a, b in pairwise(nums):
if a == key:
cnt[b] += 1
if mx < cnt[b]:
mx = cnt[b]
ans = b
return ansContinue Topic
array
Practice more edge cases under the same topic.
arrow_forwardauto_awesomeContinue Pattern
Array scanning plus hash lookup
Expand the same solving frame across more problems.
arrow_forwardsignal_cellular_altSame Difficulty Track
Easy
Stay on this level to stabilize interview delivery.
arrow_forward