Although I have read through the interesting thread Algorithm: efficient way to remove duplicate integers from an array , I have not been able to find a satisfying answer to my question:
I have a one-dimensional array of doubles, which usually is rather small (containing only up to three elements) - although this need not be a criterion for the sake of generality.
Additionally, I don't want to find only real duplicates, but duplicates in a sense that the elements' difference lies below a certain threshold. While this requirement is fairly easy to handle, my actual question is: How can I implement the general duplicate removal in ANSI C with as little overhead as possible?
Remark: The main reasons I have not been able to find my solution from the mentioned thread are threefold:
- Many of the given solutions employ languages other than pure C, so this is not of any particular help.
- Some of the solutions won't work if all elements in the array are equal, which may likely be the case in my case.
- Some of the described algorithms seem to be intended for integer values only. Being a C noob, any advice is greatly appreciated therefore.
ADDENDUM: In some kind of pseudo-code, what I would want to achieve is the following:
1) Sort the array in ascending manner
2) Loop through array until element before the last one
- Check if difference of element i to element i+1 is smaller than threshold
-> If yes, store mean value as first element of new array
-> If no, store original value of element i as first element of new array
3) Start the same again in order to check if now the differences between the new array elements lie below the threshold
-> Abort if no difference is smaller than the threshold anymore
Thus, my main problem is: How can I realize step 3 such that an arbitrary number of iterations is possible and the function runs as long as there are array elements that are "too close" (with respect to my threshold).