Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Random Pick Index problem solution

YASH PAL, 31 July 2024

In this Leetcode Random Pick Index problem solution you are 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:

  1. Solution(int[] nums) Initializes the object with the array nums.
  2. 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.
Leetcode Random Pick Index problem solution

Problem solution in Python.

class Solution(object):

    def __init__(self, nums):
        """
        :type nums: List[int]
        """
        self.nums = nums

    def pick(self, target):
        count = 0
        index = []
        for idx, num in enumerate(self.nums):
            if num == target:
                count +=1
                index.append(idx)
        return index[r]

Problem solution in Java.

HashMap<Integer, List<Integer>> map = new HashMap();
    
    public Solution(int[] nums) {
        List<Integer> list;
        for(int i = 0; i<nums.length; i++){
            list = map.getOrDefault(nums[i], new ArrayList<Integer>());
            list.add(i);
            map.put(nums[i], list);
            list = null;
        }
    }
    
    public int pick(int target) {
        List<Integer> list = map.get(target);
        double ret = Double.valueOf(list.size()) * Math.random();
        return list.get((int) ret);
    }

Problem solution in C++.

class Solution {
public:
    unordered_map<int,vector<int>>m;
    Solution(vector<int>& nums) {
        for(int i=0;i<nums.size();i++){
            m[nums[i]].push_back(i);
        }
    }
    
    int pick(int target) {         
        return m[target][rand() % m[target].size()];
    }
};

Problem solution in C.

#include <stdlib.h> 
typedef struct {
    int *array;
    int *nums;
    int numsSize;
} Solution;

Solution* solutionCreate(int* nums, int numsSize) {
    Solution* obj=(Solution*)malloc(sizeof(Solution));
    obj->array=(int*)calloc(1000,sizeof(int));
    obj->nums=nums;
    obj->numsSize=numsSize;
    return obj;
}

int solutionPick(Solution* obj, int target) {
    int count=0;
    for(int i=0;i<obj->numsSize;i++){
        if(obj->nums[i]==target){
            obj->array[count++]=i;
        }
    }
    if(count==1){
        return obj->array[count-1];
    }
    return obj->array[rand()%count];
}

void solutionFree(Solution* obj) {
    free(obj);
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes