In an array first we have to find whether a desired number exists in that or not?
If not then how will I find nearer number to the given desired number in Java?
public int nearest(int of, List 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;
}