History:
I have an array of N positive random numbers that probably contain duplicates. e.g. 10,4,5,7,10,9,10,9,8,10,5
Edit : N will probably be 32, or some other force equal to two sizes.
Problem:
I am trying to find the fastest way to replace duplicates with the missing numbers from 0- (N-1). Using the above example, I want to get a result that looks like this:
10,4,5,7,0,9,1,2,8,3,6
The goal is to have one of each number from 0 to N-1 without replacing all the numbers 0- (N-1) (random order is important). Change It is also important that this replacement is deterministic, i.e. the same input will have the same output (not random).
My decision:
Currently, Java uses 2 Boolean arrays to track used / unused numbers (unique numbers / missing numbers in the range [0, N)) and has an approximate worst-case execution time N + N * sqrt (N).
Code follows:
public byte[] uniqueify(byte[] input) { boolean[] usedNumbers = new boolean[N]; boolean[] unusedIndices = new boolean[N]; byte[] result = new byte[N]; for(int i = 0; i < N; i++) // first pass through { int newIdx = (input[i] + 128) % N; // first make positive if(!usedNumbers[newIdx]) // if this number has not been used { usedNumbers[newIdx] = true; // mark as used result[i] = newIdx; // save it in the result } else // if the number is used { unusedIndices[i] = true; // add it to the list of duplicates } } // handle all the duplicates for(int idx = 0; idx < N; idx++) // iterate through all numbers { if(unusedIndices[idx]) // if unused for(int i = 0; i < N; i++) // go through all numbers again { if(!usedNumbers[i]) // if this number is still unused { usedNumbers[i] = true; // mark as used result[i] = idx; break; } } } return result; }
It seems the fastest I can hope for, but I thought I would ask the Internet because there are people who are much smarter than me, who can have a better solution.
NB Suggestions / solutions do not have to be in Java.
Thanks.
Change I forgot to mention that I am converting this to C ++. I published my implementation of Java because it is more complete.