2312. Most Frequent Number Following Key In an Array
My accepted Python solution to LeetCode problem 2312, Most Frequent Number Following Key In an Array, running in 0ms.
- Difficulty: Easy
- Python
- Runtime 0ms
- Memory 17.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 0ms, memory 17.6MB, accepted 2026-01-01.
class Solution:
def mostFrequent(self, nums: List[int], key: int) -> int:
count = {}
for i in range(len(nums) - 1):
if nums[i] == key:
target = nums[i + 1]
count[target] = count.get(target, 0) + 1
return max(count, key=count.get)