This was what I was thinking of:
Make an array of int's, (I'll call it haveIt[]), big enough to span the range of the float array values.
For each entry in the float array, multiply the value by 100, and put the result into the haveIt array:
float value[] = {0.03, 0.82, 0.03}
int haveIt[] would then have haveIt[3] = 1 and haveIt[82] = 1, and all the rest of haveIt[] would still be set to zero.
Code:
if(haveIt[(value[n] * 100)]) //test to see if the number in value[n] is unique or not
printf("\n Not unique!");
else
printf("\n That's unique!");
The advantage of this distribution count is that it's VERY fast, just an array look up. The disadvantage is that it won't work if the values span a large range of numbers.
Here's another way:
again, make an int array, sized to your call it index[]. Initialize it like so:
Code:
for(i = 0; i < numValues; i++)
index[i] = i;
which looks silly, but you'll see it's a great programming trick.
Now use the index array to sort your values array. This is a simple sort:
Code:
for(i = 0; i < numValues -1; i++) {
for(j = i+1; j < numValues; j++) {
if(values[index[i]] < values[index[j]] {
temp = index[i];
index[i] = index[j];
index[j] = temp;
}
}
}
Now the original values have not been moved at all, but you can see them in sorted order, by going through the indeces:
Code:
for(i = 0; i < numValues; i++)
printf("%8d", values[index[i]];
Now that we have the values in sorted order through
the index, we can also do a binary
search, to find out if we have a duplicate value, or not.
Code:
for(i = 0; i < numValues; i++)
binarySearch(values, values[i]);
A binary search is simply the old "guess a number" game
you played as a kid, with a smart alecky neighbor kid:
You keep cutting the space you need to search, in half.
The advantages are that the original values are not moved at all, and it's also fast (not as super fast as the first one, but still fast.
Learning to sort through an index is one of the great tricks of programming, and so is the binary search.
if you want to go this route, let me know if you need help with the binary search function. It's simple.
All the above code is off the cuff, and untested. Consider it pseudo-code. Any problems, and I'll be glad to correct it.