Skip to content

Latest commit

 

History

History
23 lines (21 loc) · 412 Bytes

398-random-pick-index.md

File metadata and controls

23 lines (21 loc) · 412 Bytes
/**
 * @param {number[]} nums
 */
var Solution = function(nums) {
    this.data = nums;
};

/** 
 * @param {number} target
 * @return {number}
 */
Solution.prototype.pick = function(target) {
    let arr = []
    for (let i = 0; i < this.data.length; i++) {
        if (target == this.data[i]) {
            arr.push(i)
        }
    }
    return arr[Math.floor(Math.random() * arr.length)]
};