Skip to content

Instantly share code, notes, and snippets.

@SamChristy
Last active December 12, 2015 07:08
Show Gist options
  • Select an option

  • Save SamChristy/4734247 to your computer and use it in GitHub Desktop.

Select an option

Save SamChristy/4734247 to your computer and use it in GitHub Desktop.
Removes duplicate primitives from an array.
Array.prototype.unique = function(){
var map = {};
for(var i = 0; i < this.length; i++)
map[this[i]] = this[i];
this.length = 0;
for(var key in map)
this.push(map[key]);
return this; // Return to enable chaining.
}
Array.prototype.shuffle = function(){
var j, temp;
for(i = this.length-1; i > 1; i--){
j = Math.floor(Math.random() * (i + 1));
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this; // Return to enable chaining.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment