Here's the pseudocode that should be converted to any procedural language:
array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362] number = 112 print closest (number, array) def closest (num, arr): curr = arr[0] foreach val in arr: if abs (num - val) < abs (num - curr): curr = val return curr
It just works with the absolute differences between the given number and each element of the array and returns you one of them with minimal difference.
For example values:
number = 112 112 112 112 112 112 112 112 112 112 array = 2 42 82 122 162 202 242 282 322 362 diff = 110 70 30 10 50 90 130 170 210 250 | +-- one with minimal absolute difference.
As a proof of concept, here is the Python code I used to show this in action:
def closest (num, arr): curr = arr[0] for index in range (len (arr)): if abs (num - arr[index]) < abs (num - curr): curr = arr[index] return curr array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362] number = 112 print closest (number, array)
And if you really need it in Javascript, see below the full HTML file that demonstrates the function in action:
<html> <head></head> <body> <script language="javascript"> function closest (num, arr) { var curr = arr[0]; var diff = Math.abs (num - curr); for (var val = 0; val < arr.length; val++) { var newdiff = Math.abs (num - arr[val]); if (newdiff < diff) { diff = newdiff; curr = arr[val]; } } return curr; } array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; number = 112; alert (closest (number, array)); </script> </body> </html>
Now keep in mind that there may be opportunities to increase efficiency if, for example, your data is sorted (this can be done from sample data, but you did not explicitly specify this). For example, you can use binary search to find the closest item.
You should also keep in mind that if you do not need to do this many times per second, the increase in efficiency will be mostly imperceptible if your data sets are not much larger.
If you want to try this way (and you can guarantee that the array is sorted in ascending order), this is a good starting point:
<html> <head></head> <body> <script language="javascript"> function closest (num, arr) { var mid; var lo = 0; var hi = arr.length - 1; while (hi - lo > 1) { mid = Math.floor ((lo + hi) / 2); if (arr[mid] < num) { lo = mid; } else { hi = mid; } } if (num - arr[lo] <= arr[hi] - num) { return arr[lo]; } return arr[hi]; } array = [2, 42, 82, 122, 162, 202, 242, 282, 322, 362]; number = 112; alert (closest (number, array)); </script> </body> </html>
Mostly used bracketing and checking the average value to reduce the solution space by half for each iteration, the classic O(log N) algorithm, while the sequential search was O(N) :
0 1 2 3 4 5 6 7 8 9 <- indexes 2 42 82 122 162 202 242 282 322 362 <- values LMHL=0, H=9, M=4, 162 higher, H<-M LMHL=0, H=4, M=2, 82 lower/equal, L<-M LMHL=2, H=4, M=3, 122 higher, H<-M LHL=2, H=3, difference of 1 so exit ^ | H (122-112=10) is closer than L (112-82=30) so choose H
As already mentioned, this should not matter much for small datasets or for things that don't need to be dazzlingly fast, but this is an option that you might want to consider.