title: deleting duplicates on sorted array
body: <p>Just in case you missed, the question is about deleting duplicates on a <code>sorted</code> array. Which can be applied very fast algorithms (compared to unsorted arrays) to remove duplicates.</p>
  
  <ul>
  <li>You can skip this if you already know how deleting duplicates on SORTED arrays work</li>
  </ul>
  
  <p><strong>Example:</strong></p>
  
  <pre><code>var out=[];
  for(var i=0,len=arr.length-1;i&lt;len;i++){
      if(arr[i]!==arr[i+1]){
          out.push(arr[i]);
      }
  }
  out.push(arr[i]);
  </code></pre>
  
  <p>See?, it is very fast. I will try to explain what just happened.</p>
  
  <p>The sorted arrays *could look like this:</p>
  
  <pre><code>arr=[0,1,1,2,2,3,4,5,5,6,7,7,8,9,9,9];
  </code></pre>
  
  <p>*the sorting could be ASC or DESC, or by other weird methods, but the important thing is that every duplicated item is next each other.</p>
  
  <p>We stopped at <code>array.length-1</code> because we don't have anything to check with</p>
  
  <p>Then we added the last element regardless of anything because:</p>
  
  <p>case A:</p>
  
  <p><code>... ,9,9,9];//we have dup(s) on the left of the last element</code></p>
  
  <p>case B:</p>
  
  <p><code>... ,7,9,10];//we don't have dup(s) on the left of the last element</code></p>
  
  <p>If you really understand what is happening, you will know that we haven't added any <code>9</code> on the case A. So because of that, we want to add the last element no matter if we are on case A or B.</p>
  
  <hr>
  
  <p><strong>Question:</strong></p>
  
  <p>That explained, I want to do the same, but ignoring the <code>undefined</code> value on cases like:</p>
  
  <pre><code>var arr=[];arr[99]=1;//0 through 98 are undefined, but do NOT hold the undefined value
  </code></pre>
  
  <p>I want to remove those. And on the case I have some real <code>undefined</code> values, these should not be removed.</p>
  
  <p>My poor attempt is this one:</p>
  
  <pre><code>var out=[];
  for (var i=0,len=arr.length; i &lt; len - 1;) {
    var x = false;
    var y = false;
  
    for (var j = i, jo; j &lt; len - 1; j++) {
      if (j in arr) {
        x = true;
        jo = arr[j];
        i = j + 1;
        break;
      }
    }
    if (x == false) {
      break;
    }
  
    for (var u = i, yo; u &lt; len - 1; u++) {
      if (u in arr) {
        y = true;
        yo = arr[u];
        i = u + 1;
        break;
      }
    }
    if (y == false) {
      out.push(jo);
      break;
    }
  
    if (jo !== yo) {
      out.push(jo);
    }
  }
  out.push(arr[len - 1]);
  </code></pre>
  
  <p>I am really lost, any help is appreciated</p>