The following is a question from Cracking the coding interview:
You have an array with all numbers from 1 to N, where N is at most 32,000. The array may have duplicate entries and you do not know what N is. With only 4KB of memory available, how would you print all duplicate elements in the array?
The method signature is
public static void checkDuplicates(int[] array)
Then the solution explains how you can use bit vector to solve this by representing each integer as a bit. My confusion is when we run this method, won't it load the entire array in memory to loop through it? Now if the array
is of size say,for example, 1 billion (many repeated elements) won't this program fail since it loads the entire array in memory and the memory we have is 32 * 2^10
bits?