Most Frequent Number Following Key In an Array

给一个数组和一个数字key, 求当数组中一个元素等于key的时候, 下一个元素的出现频数最大的数字.

class Solution {
    public int mostFrequent(int[] nums, int key) {
        int[] count = new int[1001];
        for(int i = 0; i < nums.length - 1; i++){
            if(nums[i] == key)
                count[nums[i + 1]]++;
        }
        int max = -1;
        int max_index = -1;
        for(int i = 0; i < 1001; i++){
            if(count[i] > max){
                max = count[i];
                max_index = i;
            }
        }
        return max_index;
    }
}