Skip to content

Instantly share code, notes, and snippets.

@denzildoyle
Created October 6, 2015 17:38
Show Gist options
  • Save denzildoyle/5d45590766d4ecc1dee8 to your computer and use it in GitHub Desktop.
Save denzildoyle/5d45590766d4ecc1dee8 to your computer and use it in GitHub Desktop.
Remove duplications from array in JavaScript
function removeDuplicates(array) {
var newArray = []; //create new array to store no duplicates
var isDuplicated = false; //track duplicate indexes
newArray.push(array[0]); //add first index to new array
for (i=1; i <= array.length; i++) { //loop over entered array
for (var x = newArray.length - 1; x >= 0; x--) { // loop over new array
if (newArray[x] == array[i]){ //compare newArray to entered array to see if the current index value already exist in the array
isDuplicated = true; //current array index has a duplicate value
break;
} else if (isDuplicated == false && x == 0 && array[i] != null) { //last index in array checked and the current index value is not a duplicate
newArray.push(array[i]); //add value to new array
}
}
}
return newArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment