http://qs321.pair.com?node_id=659011


in reply to Find out missing floating value in an array

But that isn't an increment of 0.1. 1.2 is in your list twice, and 1.15 doesn't belong there at all. Are you really talking about floating values, or are you talking about some kind of version number or the like?

Anyway, the algorithm is easy. Given the increment and the array, take the starting value in the array and add the increment to it to form your expected value. Then loop through the array from the second element on: for each element, while it's more than the expected value, put the expected value in your list of missing values and increment it. If the array element is equal to your expected value, increment the expected value and go on to the next array element.

Where I say "more than" or "equal to", allow for floating point imprecision and rounding error.

Alternatively, collect all values equal to the starting value plus some multiple of the increment that are between the starting and ending values (perhaps as hash keys), then remove all those that appear in your array. What's left are the missing values.

If the increment isn't provided, you could initially take it as the difference between the first two elements in the array, then correct it if you find values that don't work with that increment.

  • Comment on Re: Find out missing floating value in an array