How can I randomly remove a key with value 0 efficiently ?
Dictionary<string, int> dict = new Dictionary<Edge, int>();
dict.add("a",0);
dict.add("b",0);
dict.add("c",0);
dict.add("d",1);
The size of dictionary is 10000.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Something like this should do it:
To generate a random index, you could use:
Find the indexToRemove th element from pairsToRemove and remove it from the dictionary.
As to efficiency:
The complexity should be O(n)[get all items with value 0] + O(.6N)[finding ith value to remove] + O(log(n))[deletion] assuming the random number generation is constant time.
The problem is, there is no way to perform a value lookup on a dictionary in better than O(n) time. So that will be your bottleneck.