There was a task to find several numbers closest to the transferred value from the huge amount of data stored in the array. Found such an option. https://stackoverflow.com/questions/1187352/find-closest-value-in-an-ordererd-list It can be adapted by removing the closest value from the array for each pass, but this is too long. How to optimize what would the search go for the minimum number of passes?
public int closest(int of, List<Integer> in) { int min = Integer.MAX_VALUE; int closest = of; for (int v : in) { final int diff = Math.abs(v - of); if (diff < min) { min = diff; closest = v; } } return closest; }