Simple Python solution

• Step 1: define an empty list ,`self.r = []`

Step 2: iterate through the array and search for indices where the value matches `target`

Step 3: append these indices into empty list, `self.r`

Step 4: pick a random element from the list, using randint function.

Step 5: reset `self.r=[]`, incase this function is used twice. i.e for one object, [1,2,3,3,3,4,4,4,4] the user can run function the functopm twice.
for example:

``````s = Solution([0,1,2,3,3,3])
print s.pick(3)
print s.pick(4)
``````

Solution:

``````from random import randint
class Solution(object):

def __init__(self, nums):
"""

:type nums: List[int]
:type numsSize: int
"""
self.nums = nums
self.numsSize = len(nums)
self.r = []

def pick(self, target):
"""
:type target: int
:rtype: int
"""
for i in xrange(self.numsSize):
if self.nums[i] == target:
self.r.append(i)

x = self.r[randint(0,len(self.r)-1)]
self.r = []
return x

``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.