Most Frequent Number follwoing a Key in Array

less than 1 minute read

Problem Statement

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.

Constraints:
2 <= nums.length <= 1000
1 <= nums[i] <= 1000
The test cases will be generated such that the answer is unique.

Solution I

from Collections import Counter

def mostFrequent(nums):
    count = Counter()
    for i, n in enumerate(nums):
        if n == key and i<len(nums)-1:
            count[nums[i+1]] += 1
    
    return count.most_common()[0][0]

Solution II

def mostFrequent(arr, key):
    num = [0]*1001
    for i in range(len(arr)-1):
        if arr[i] == key:
            num[arr[i+1]] += 1
    
    return num.index(max(num))

Conclusion

Time Complexity is O(N) for one pass of list and Space Complexity is O(N) for creating hashmap.

Updated:

Leave a comment