Dev HelpersOpen library
← Back

Find the first boundary in a sorted array

Find the first boundary in a sorted array in JavaScript.

Open this example in the interactive library

Source code

const values = [1, 3, 3, 7];
const target = 3;
let low = 0,
  high = values.length;
while (low < high) {
  const mid = Math.floor((low + high) / 2);
  if (values[mid] < target) low = mid + 1;
  else high = mid;
}
console.log(low);

Sample output

1