Finding float elements have the same value in an array
Moderators - Reputation: 9942
Posted 18 December 2012 - 05:23 AM
An algorithmically quicker method is to sort the array first (and preserve the original index information if you need that) and then only look for adjacent element with equal values.
Note that once you introduce an epsilon in your comparison, you may get some non-intuitive results. Your code may say that elements x and y are equal and that y and z are equal, but that x and z are not equal. The epsilon-comparison is not transitive.
Members - Reputation: 508
Posted 18 December 2012 - 05:39 AM
In the first step I would create an int array to store all the indexes for each value in the float array and create an exact copy of the float array(assuming you still want it to be intact and have the values not being moved around).
I would then implement some sorting algorithm(which one I decide to use depends on the amount of numbers and how important the efficiency is required to be). This algorithm will then sort from smallest to largest in my copy of the float array and at the same time swap the corresponding value in the int array I created.
When this is all done my copy of floats will store its values from smallest to largest and my int array will have the indexes of these values corresponding to their initial value in the original float array. Then I would need to check the first element in my copy and compare it to the next element. If they are not the same I would stop checking that first element since the array will be stored in order of size. If the next element isn't the same size then there are no values that are the same size as this one. I would then continue with the next element.
1. Create a float array which will be a copy of your float array to check. Also create an int array to store the indexes of the float array. This int array will simply have 1,2,3,4,5 etc. The use of the int array will come when sorting the float array.
2. Implement a sorting algorithm to sort the copy of the float array by size. Move their corresponding index value in the int array. Example: number 38.2 is on index 3. 38.2 is the lowest number generated. Move the number 38.2 to position float array in your copy. Move the value 3 to position int array in the int array.
3. Go through each index in your sorted array and compare it to the element next in that array. If that value is not equal then your element does not appear more than once in your array. Go to the next element, i.e element . Compare it against element  and if they are the same, then do what ever you are supposed to do if there are more than one of an element. In the int array at position  the value of its corresponding index in the original float array will be listed.
Note: This is a solution on top of my head and is quite a lot of work ^^ There are probably several other efficient ways of doing this, but this is the first solution that came to mind.
Prime Members - Reputation: 1581
Posted 18 December 2012 - 04:34 PM
int x, y; float array[ ARRAY_SIZE ]; for(x = 0; x < ARRAY_SIZE; x++) for(y = x+1; y < ARRAY_SIZE; y++) if(fabs(array[x] - array[y]) < EPSILON) printf("array[%d] (%f) ~ array[%d] (%f)\n", x, array[x], y, array[y]);
Edited by radioteeth, 02 January 2013 - 11:49 PM.