You can easily open 512 files, so you can preview both sets of 256 pieces on disk, with a maximum of 16 M items, each 64 megabytes in size. You can do this based on the most significant byte of each Long, from set.A.00 to set.B.ff
Then you can load each corresponding pair of pieces ( set.A.42 containing Longs, starting at 0x42, corresponds to set.B.42 ) and use them to initialize the Byte 16M array - you initialize it for all 0s and when you read the value i from the first fragment, you increase the i-th index). Then you read in the second fragment, but this time increase by 2.
Upon completion, you will check the array; 0 means that the value was not present in any fragment, 1 means that it was present only in the first set, 2 only in the second, 3 in both. And write the results to the result file.
You do not need sorting, even if the result file is sorted (since you will check the pieces in order and do the final scan in order).
All this is done in O (n) (all steps are performed in linear time) and no more than 16M RAM is required.
If there are too many 512 open files, you can use the first 7 most significant bits and use 256 open files and 32 MB of RAM. Or 128 open files and 64 M RAM, etc.
It is also possible (and perhaps more efficient if the file system cache is not too good) to save a series of 256 "buckets", each of which has a size of 16384 Longs (so it is again 16M). When the bucket reaches its full value, you will open the corresponding chunk file on the disk, unload the 16384 Long so far found and close the file. Then do the same for set B. You end up with 512 files containing from 0 (unlikely) to 16 million Long s, never opening more than two files at a time.
source share