Skip to content

Instantly share code, notes, and snippets.

@alloyking
Created November 27, 2012 14:29
Show Gist options
  • Save alloyking/4154506 to your computer and use it in GitHub Desktop.
Save alloyking/4154506 to your computer and use it in GitHub Desktop.
Remove duplicates from array
var myArray = ["bar", "foo", "zorb", "bar", "baz", "fum", "baz"];
Array.prototype.removeDuplicates = function() {
var input = this;
var hashObject = new Object();
for (var i = input.length - 1; i >= 0; i--) {
var currentItem = input[i];
if (hashObject[currentItem] == true) {
input.splice(i, 1);
}
hashObject[currentItem] = true;
}
return input;
}
myArray.removeDuplicates();
alert(myArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment