This function checks an array for duplicated values. If it finds a duplicate value, it increments the variable name repeatedTime
and then if repeatedTime
is greater than 1 it calls a randomize function which produce different values in the array that was passed to the function. It repeats these steps until a duplicated value free array is produced and then notDuplicate
becomes true and the function ends.
Are there any improvements that can be made to the function? I am thinking about making it into a recursive function. Is that feasible?
private void CheckForDuplicates(int[] selectedWordIds, List<Integer> answersTempId) {
boolean notDuplicate = false;
while(notDuplicate != true){
int repeatedTime = 0;
for(int x = 0; x < selectedWordIds.length ; x++)
{
for(int y =0; x < selectedWordIds.length;x++)
{
if(selectedWordIds[x] == selectedWordIds[y])
{
repeatedTime++;
}
}
}
if(repeatedTime > 1)
{
notDuplicate = false;
RandomizeFunction(selectedWordIds, answersTempId);
}
else{
notDuplicate = true;
}
}
}
answersTempId
? Perhaps it plays a role in yourRandomization
method however that is not clear. \$\endgroup\$ – nattyddubbs Dec 29 '14 at 20:03