Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static int findValueClosestTo(int value, byte[] allowedValues) { int smallestDistance = Math.abs(allowedValues[0] - value); int index = 0; for (int i = 1; i < allowedValues.length; i++) { int currDistance = Math.abs(allowedValues[i] - value); if (currDistance < smallestDistance) { index = i; smallestDistance = currDistance; } } return allowedValues[index]; } }