<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: Fast Algorithm To Find Unique Items in JavaScript Array</title> <atom:link href="http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/feed/" rel="self" type="application/rss+xml" /><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/</link> <description>All you need to know what I know</description> <lastBuildDate>Fri, 10 Feb 2012 05:52:00 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Neumonicom</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4170</link> <dc:creator>Neumonicom</dc:creator> <pubDate>Fri, 10 Feb 2012 05:52:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4170</guid> <description>Consider this variation as well.  This will preserve any data type without sorting the results.  Try running some tests on this to see if its faster.  One thing worth noting here is that native methods have been optimized, so avoiding as much conditional logic and loops as possible is a good thing:
Array.prototype.unique = function(){
var len = this.length,
a = [],
item,
i=0;
for(;i&lt;len;){
item = this[i++];
if(a.indexOf(item) === -1)a.push(item);
}
return a;
} </description> <content:encoded><![CDATA[<p>Consider this variation as well.  This will preserve any data type without sorting the results.  Try running some tests on this to see if its faster.  One thing worth noting here is that native methods have been optimized, so avoiding as much conditional logic and loops as possible is a good thing:</p><p>Array.prototype.unique = function(){<br
/> var len = this.length,<br
/> a = [],<br
/> item,<br
/> i=0;</p><p> for(;i&lt;len;){<br
/> item = this[i++];<br
/> if(a.indexOf(item) === -1)a.push(item);</p><p> }<br
/> return a;<br
/> } </p> ]]></content:encoded> </item> <item><title>By: Shamasis Bhattacharya</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4169</link> <dc:creator>Shamasis Bhattacharya</dc:creator> <pubDate>Fri, 10 Feb 2012 05:40:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4169</guid> <description>This will fail on Google Chrome on arrays with many items. The WebKit insertion sort is not stable for equality sort. :(</description> <content:encoded><![CDATA[<p>This will fail on Google Chrome on arrays with many items. The WebKit insertion sort is not stable for equality sort. <img
src='http://www.shamasis.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>By: Neumonicom</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4168</link> <dc:creator>Neumonicom</dc:creator> <pubDate>Fri, 10 Feb 2012 05:37:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4168</guid> <description>This is probably the fastest and most reliable way to create a unique array whose members can be of any object.  The only downside is that it will sort the results.  Perhaps there is a workaround?
Array.prototype.unique = function(){
this.sort(function(a,b){
if(a===b)return 0;
return 1;
});
var length = this.length;
while(length--)if(this[length] === this[length-1])this.splice(length,1);
return this;
} </description> <content:encoded><![CDATA[<p>This is probably the fastest and most reliable way to create a unique array whose members can be of any object.  The only downside is that it will sort the results.  Perhaps there is a workaround?</p><p>Array.prototype.unique = function(){<br
/> this.sort(function(a,b){<br
/> if(a===b)return 0;<br
/> return 1;</p><p> });<br
/> var length = this.length;<br
/> while(length&#8211;)if(this[length] === this[length-1])this.splice(length,1);<br
/> return this;<br
/> } </p> ]]></content:encoded> </item> <item><title>By: Neumonicom</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4167</link> <dc:creator>Neumonicom</dc:creator> <pubDate>Fri, 10 Feb 2012 05:28:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4167</guid> <description>Sorry, I meant primitives instead of object literals in the first line.  &#039;a 1 0 2&#039;.</description> <content:encoded><![CDATA[<p>Sorry, I meant primitives instead of object literals in the first line.  &#8217;a 1 0 2&#8242;.</p> ]]></content:encoded> </item> <item><title>By: Neumonicom</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4166</link> <dc:creator>Neumonicom</dc:creator> <pubDate>Fri, 10 Feb 2012 05:27:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4166</guid> <description>That will only work for values that are object literals.  It will not work for objects, as their internal .toString() method will convert them to something like :[object Object].  Thus if you have multiple objects with the same constructor you will continually overwrite the last one.</description> <content:encoded><![CDATA[<p>That will only work for values that are object literals.  It will not work for objects, as their internal .toString() method will convert them to something like :[object Object].  Thus if you have multiple objects with the same constructor you will continually overwrite the last one.</p> ]]></content:encoded> </item> <item><title>By: Shamasis Bhattacharya</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4165</link> <dc:creator>Shamasis Bhattacharya</dc:creator> <pubDate>Fri, 10 Feb 2012 04:29:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4165</guid> <description>It was actually very stupid on my part to think in this direction! The fastest algo actually does not take into account array order. We could simply do the negative looping here.
&lt;pre&gt;Array.prototype.unique = function () {
    var o = {}, i = this.length, r = [];
    while (i -= 1) o[this[i]] = this[i];
    for (i in o) r.push(o[i]);
    delete o;
    return r;
};&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>It was actually very stupid on my part to think in this direction! The fastest algo actually does not take into account array order. We could simply do the negative looping here.</p><p><pre>Array.prototype.unique = function () {
    var o = {}, i = this.length, r = [];
    while (i -= 1) o[this[i]] = this[i];
    for (i in o) r.push(o[i]);
    delete o;
    return r;
};</pre></p> ]]></content:encoded> </item> <item><title>By: Shamasis Bhattacharya</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4164</link> <dc:creator>Shamasis Bhattacharya</dc:creator> <pubDate>Fri, 10 Feb 2012 04:04:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4164</guid> <description>Yeah! I always tend to have a fascination towards the &quot;coolKid&quot; variants. Sadly, the overhead of an in-loop function call scares me. :-/</description> <content:encoded><![CDATA[<p>Yeah! I always tend to have a fascination towards the &#8220;coolKid&#8221; variants. Sadly, the overhead of an in-loop function call scares me. :-/</p> ]]></content:encoded> </item> <item><title>By: Shamasis Bhattacharya</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4163</link> <dc:creator>Shamasis Bhattacharya</dc:creator> <pubDate>Fri, 10 Feb 2012 04:02:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4163</guid> <description>You are correct in noting that. :) In fact, to retain data-type, it becomes a bit more complex where we can create a hash for each type. Then again, we will loose the original array item order. To retain the order, the amount of computation required will defeat the whole purpose of this method! :(</description> <content:encoded><![CDATA[<p>You are correct in noting that. <img
src='http://www.shamasis.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> In fact, to retain data-type, it becomes a bit more complex where we can create a hash for each type. Then again, we will loose the original array item order. To retain the order, the amount of computation required will defeat the whole purpose of this method! <img
src='http://www.shamasis.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /></p> ]]></content:encoded> </item> <item><title>By: Aurelio Jargas</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4162</link> <dc:creator>Aurelio Jargas</dc:creator> <pubDate>Thu, 09 Feb 2012 23:43:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4162</guid> <description> Thanks for the post!
Just note that the two codes you posted are not 100% similar. The fastest version will consider 1 and &quot;1&quot; equal, removing one of them. The classic keeps both in output.
&lt;pre&gt;
[1,2,3,&quot;1&quot;,&quot;4&quot;].unique_fastest()
[&quot;1&quot;, 2, 3, &quot;4&quot;]
[1,2,3,&quot;1&quot;,&quot;4&quot;].unique_classic()
[1, 2, 3, &quot;1&quot;, &quot;4&quot;]
&lt;/pre&gt;</description> <content:encoded><![CDATA[<p>Thanks for the post!</p><p>Just note that the two codes you posted are not 100% similar. The fastest version will consider 1 and &#8220;1&#8243; equal, removing one of them. The classic keeps both in output.</p><p><pre>
[1,2,3,&quot;1&quot;,&quot;4&quot;].unique_fastest()
[&quot;1&quot;, 2, 3, &quot;4&quot;]
[1,2,3,&quot;1&quot;,&quot;4&quot;].unique_classic()
[1, 2, 3, &quot;1&quot;, &quot;4&quot;]
</pre></p> ]]></content:encoded> </item> <item><title>By: Brandon Benvie</title><link>http://www.shamasis.net/2009/09/fast-algorithm-to-find-unique-items-in-javascript-array/comment-page-1/#comment-4136</link> <dc:creator>Brandon Benvie</dc:creator> <pubDate>Fri, 28 Oct 2011 09:56:00 +0000</pubDate> <guid
isPermaLink="false">http://www.shamasis.net/?p=976#comment-4136</guid> <description>Array.prototype.unique = function(){
return this.filter(function(s, i, a){ return i == a.lastIndexOf(s); });
}
You loop through the array at most an average of 50% of the length of the array per item,  when there&#039;s zero doubles. The more items filtered the more efficient it is. Usually doesn&#039;t beat the hash method  but it has its place. It wins on style points though cause sometimes looking pretty is better than being smart.</description> <content:encoded><![CDATA[<p>Array.prototype.unique = function(){<br
/> return this.filter(function(s, i, a){ return i == a.lastIndexOf(s); });<br
/> }</p><p>You loop through the array at most an average of 50% of the length of the array per item,  when there&#8217;s zero doubles. The more items filtered the more efficient it is. Usually doesn&#8217;t beat the hash method  but it has its place. It wins on style points though cause sometimes looking pretty is better than being smart.</p> ]]></content:encoded> </item> </channel> </rss>
