performance - Best algorithm to find N unique random numbers in VERY large array -


i have array with, example, 1000000000000 of elements (integers). best approach pick, example, 3 random , unique elements array? elements must unique in whole array, not in list of n (3 in example) elements.

i read reservoir sampling, provides method pick random numbers, can non-unique.

if odds of hitting non-unique value low, best bet select 3 random numbers array, check each against entire array ensure unique - if not, choose random sample replace , repeat test.

if odds of hitting non-unique value high, increases number of times you'll need scan array looking uniqueness , makes simple solution non-optimal. in case you'll want split task of ensuring unique numbers task of making random selection.

sorting array easiest way find duplicates. sorting algorithms o(n log n), since keys integers radix sort can potentially faster.

another possibility use hash table find duplicates, require significant space. can use smaller hash table or bloom filter identify potential duplicates, use method go through smaller list.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -