Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 and,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:
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.
Constraints:
2 <= nums.length <= 10001 <= nums[i] <= 1000Problem summary: 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 and, 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,100,200,1,100] 1
[2,2,2,2,3] 2
sort-array-by-increasing-frequency)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2190: Most Frequent Number Following Key In an Array
class Solution {
public int mostFrequent(int[] nums, int key) {
int[] cnt = new int[1001];
int ans = 0, mx = 0;
for (int i = 0; i < nums.length - 1; ++i) {
if (nums[i] == key) {
if (mx < ++cnt[nums[i + 1]]) {
mx = cnt[nums[i + 1]];
ans = nums[i + 1];
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2190: Most Frequent Number Following Key In an Array
func mostFrequent(nums []int, key int) (ans int) {
cnt := [1001]int{}
mx := 0
for i, x := range nums[1:] {
if nums[i] == key {
cnt[x]++
if mx < cnt[x] {
mx = cnt[x]
ans = x
}
}
}
return
}
# Accepted solution for LeetCode #2190: Most Frequent Number Following Key In an Array
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 ans
// Accepted solution for LeetCode #2190: Most Frequent Number Following Key In an Array
/**
* [2190] 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 and,
* 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:
*
* 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.
*
*
* Constraints:
*
* 2 <= nums.length <= 1000
* 1 <= nums[i] <= 1000
* The test cases will be generated such that the answer is unique.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/
// discuss: https://leetcode.com/problems/most-frequent-number-following-key-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn most_frequent(nums: Vec<i32>, key: i32) -> i32 {
nums.windows(2)
.fold(
std::collections::HashMap::<_, u32>::with_capacity(nums.len()),
|mut map, win| {
if win[0] == key {
map.entry(win[1]).and_modify(|v| *v += 1).or_default();
}
map
},
)
.into_iter()
.max_by_key(|(_, v)| *v)
.unwrap()
.0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2190_example_1() {
let nums = vec![1, 100, 200, 1, 100];
let key = 1;
let result = 100;
assert_eq!(Solution::most_frequent(nums, key), result);
}
#[test]
fn test_2190_example_2() {
let nums = vec![2, 2, 2, 2, 3];
let key = 2;
let result = 2;
assert_eq!(Solution::most_frequent(nums, key), result);
}
}
// Accepted solution for LeetCode #2190: Most Frequent Number Following Key In an Array
function mostFrequent(nums: number[], key: number): number {
const cnt: number[] = Array(Math.max(...nums) + 1).fill(0);
let [ans, mx] = [0, 0];
for (let i = 0; i < nums.length - 1; ++i) {
if (nums[i] === key) {
if (mx < ++cnt[nums[i + 1]]) {
mx = cnt[nums[i + 1]];
ans = nums[i + 1];
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.