LeetCode solutions

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

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.6MB, accepted 2026-01-01.

python
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)

Source