title: Renumber a sequence to remove gaps, but keep identical numbers
body: <p>Assume an unordered list of numbers, with duplicates being allowed. I want to patch all gaps or sudden jumps in it. Some examples:</p>
  
  <pre class="lang-py prettyprint-override"><code>def renum(arr):
      # magic happens here
      pass
  
  renum(np.array([1, 1, 1, 2, 2, 2]))  # already in correct shape
  &gt; [1, 1, 1, 2, 2, 2]
  
  renum(np.array([1, 1, 2, 2, 4, 4, 5, 5, 5]))  # A jump between 2 and 4
  &gt; [1,1, 2, 2, 3, 3, 4, 4, 4]
  
  renum(np.array([1, 1, 2, 2, 5, 2, 2]))  # A forward and backward jump
  &gt; [1,1, 2, 2, 3, 4, 4]
  </code></pre>
  
  <p>Finding gaps is easy, but I have a hard time when trying to renumber gaps followed by the same number multiple times when processing the sequence elementwise. I.e the attempt below fails because numbers can occur many times:</p>
  
  <pre class="lang-py prettyprint-override"><code>def renum(arr):
      new_arr = np.zeros(len(arr))
      prev_num = new_arr[0]
      for idx, num in enumerate(arr):
          diff = num - prev_num
          if diff == 0 or diff == 1:
              new_arr[idx] = num
          else:
              new_arr[idx] = prev_num + 1
          prev_num = new_arr[idx]
      return new_arr
  
  renum(np.array([1, 1, 2, 2, 4, 4, 5, 5, 5]))
  &gt; [1, 1, 2, 2, 3, 4, 5, 5, 5]  # should actually be [1, 1, 2, 2, 3, 3, 4, 4, 4]
  </code></pre>
  
  <p>Also I think this implementation is not very efficient..
  Any ideas?</p>