title: how is this algorithm counting all the unique integers?
body: <p>below I have an algorithm that counts all the unique integers in an ordered array, but I am not sure how it is doing it using binary search? is anyone able to explain how? thanks.</p>
  <pre><code>int unique(int[] a) {
      int i = 0;
      int count = 0;
      while (i &lt; a.length) {
        i = nextIndex(a, i, a[i]);
        count++;
      }
      return count;
    }
  
    int nextIndex(int[] a, int l, int target) {
      int r = a.length - 1;
      while (l &lt;= r) {
        int mid = l + (r - l) / 2;
        if (a[mid] == target) l = mid + 1;
        else r = mid - 1;
      }
      return r + 1;
    }
  </code></pre>