I've been trying to write code that finds the unique values in a sorted array that also has duplicates.
So far, I've written:
public static int numUnique (double[] list) {
int counter = 0;
if (Array.getLength(list) < 1) {
return Length.list);
}
else{
for (int i=0; i < Length.list); i++){
for (int j=i+1; j< Length.list); j++){
if (list[i] != list[j]){
newrArray[i] = list[i];
counter++;
}
}
}
}
return counter;
}
Input:
{31, 31, 31, 31, 33, 46, 46, 46, 46, 46, 52, 65, 65, 66, 75, 98, 98}
Expected output:
8
I cannot use HashSets or ArrayLists. I think the only viable option is copying from one array to another and then counting what is in the new array (assuming that only unique values are copied into the new array).