Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.

Implement the Solution class:


	Solution(int[] nums) Initializes the object with the array nums.
	int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i's, then each index should have an equal probability of returning.


&nbsp;
Example 1:

Input
["Solution", "pick", "pick", "pick"]
[[[1, 2, 3, 3, 3]], [3], [1], [3]]
Output
[null, 4, 0, 2]

Explanation
Solution solution = new Solution([1, 2, 3, 3, 3]);
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.
solution.pick(1); // It should return 0. Since in the array only nums[0] is equal to 1.
solution.pick(3); // It should return either index 2, 3, or 4 randomly. Each index should have equal probability of returning.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 2 * 104
	-231 &lt;= nums[i] &lt;= 231 - 1
	target is an integer from nums.
	At most 104 calls will be made to pick.


class Solution:

    def __init__(self, nums: List[int]):
        #self.nums = nums
        #create a hash of values with their list of indices
        self.map = defaultdict(list)
        for i,v in enumerate(nums):
            self.map[v].append(i)
        

    def pick(self, target: int) -> int:
        return random.sample(self.map[target],1)[0]
        '''
        reservoir = 0
        count = 0
        for i in range(len(self.nums)):
            if self.nums[i] == target:
                count+=1
                if random.random() < 1/count:
                    reservoir = i
        return reservoir

        
        samp = []
        for i in range(len(self.nums)):
            if self.nums[i] == target:
                samp.append(i)
        return (random.sample(samp,1))[0]
        '''
        


# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.pick(target)
class Solution {
    ArrayList<Integer> ll=new ArrayList<>();
    public Solution(int[] nums) {
        for(int i=0;i<nums.length;i++){
            ll.add(nums[i]);
        
        }
    }
    public int pick(int target) {
        double a=Math.random();
        int n=(int)(a*this.ll.size());
        while(this.ll.get(n)!=target){
            a=Math.random();
            n=(int)(a*this.ll.size());
        }
        return n; 
    }
}
class Solution {
    unordered_map<int,vector<int>> itemIndicies;
public:
    Solution(vector<int>& nums)
    {
        srand(time(NULL));
        
        for (int i = 0; i < nums.size(); i++)
        {
            if (itemIndicies.find(nums[i]) == itemIndicies.end())
                itemIndicies[nums[i]] = {i};
            else
                itemIndicies[nums[i]].push_back(i);
        }
    }
    
    int pick(int target) {
        int size = itemIndicies[target].size();
        int randomValue = rand() % size;
        return itemIndicies[target][randomValue];
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(nums);
 * int param_1 = obj->pick(target);
 */
var Solution = function(nums) {
    this.map = nums.reduce((result, num, index) => {
        const value = result.get(num) ?? [];

        value.push(index);
        result.set(num, value);
        return result;
    }, new Map());
};

Solution.prototype.pick = function(target) {
    const pick = this.map.get(target);
    const random = Math.random() * pick.length | 0;

    return pick[random];
};
Random Pick Index