title: Append array element only if it is not already there in Javascript
body: <p>I need to add an element to an array only if it is not already there in Javascript.  Basically I'm treating the array as a set.  </p>
  
  <p>I need the data to be stored in an array, otherwise I'd just use an object which can be used as a set.</p>
  
  <p>I wrote the following array prototype and wanted to hear if anyone knew of a better way.  This is an O(n) insert.  I was hoping to do O(ln(n)) insert, however, I didn't see an <strong>easy</strong> way to insert an element into a sorted array.  For my applications, the array lengths will be very small, but I'd still prefer something that obeyed accepted rules for good algorithm efficiency:</p>
  
  <pre><code>Array.prototype.push_if_not_duplicate = function(new_element){
      for( var i=0; i&lt;this.length; i++ ){
          // Don't add if element is already found
          if( this[i] == new_element ){
              return this.length;
          }
      }
      // add new element
      return this.push(new_element);
  }
  </code></pre>