Posted by
Jeffye |
11:49 AM
Javascript removes deplicates in an array
01 | Array.prototype.delRepeat= function (){ |
02 | var newArray= new Array(); |
04 | for ( var i=0;i<len ;i++){ |
05 | for ( var j=i+1;j<len;j++){ |
06 | if ( this [i]=== this [j]){ |
10 | newArray.push( this [i]); |
Obviously, there are two 'for' in this code snippet, which makes it very inefficient. Here is a more efficient one:
01 | Array.prototype.delRepeat= function (){ |
03 | var provisionalTable = {}; |
04 | for ( var i = 0, item; (item= this [i]) != null ; i++) { |
05 | if (!provisionalTable[item]) { |
07 | provisionalTable[item] = true ; |
Post a Comment