Created
November 12, 2010 23:27
-
-
Save rwaldron/674893 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var states = ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Dakota", "North Carolina", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"], | |
| mega = [], | |
| howmany = 100; | |
| while(howmany--) { | |
| mega = mega.concat(states); | |
| } | |
| // Generally, I see implementations that look like this... | |
| Array.prototype.uniquedual = function() { | |
| var o = {}, i, l = this.length, r = []; | |
| for(i=0; i<l;i++) o[this[i]] = this[i]; | |
| for(i in o) r.push(o[i]); | |
| return r; | |
| }; | |
| console.log( mega.uniquedual() ); | |
| // Which got me thinking. And that resulted in this... | |
| // It combines the tasks of the two loops into one | |
| Array.prototype.unique = function() { | |
| var o = {}, r = [], i = 0, l = this.length; | |
| while (l--) { | |
| i++; | |
| !o[this[i]] && ( r[r.length] = this[i] ); | |
| o[this[i]] = this[i]; | |
| } | |
| return r; | |
| }; | |
| console.log( mega.unique() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment