398. Random Pick Index (Medium) - TengnanYao/daily_leetcode GitHub Wiki

class Solution:

    def __init__(self, nums: List[int]):
        self.h = {}
        for i, num in enumerate(nums):
            self.h[num] = self.h.get(num, []) + [i]

    def pick(self, target: int) -> int:
        return random.choice(self.h[target])

# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)