Skip to content

Instantly share code, notes, and snippets.

@phillypb
Created October 18, 2022 08:37
Show Gist options
  • Save phillypb/9b6720720ba44dfd44e354ba1138016c to your computer and use it in GitHub Desktop.
Save phillypb/9b6720720ba44dfd44e354ba1138016c to your computer and use it in GitHub Desktop.
/*
Sort through an array and remove any duplicate values.
Does not account for changes in case-sensitivity.
*/
function removeDuplicates() {
// array of duplicates
var rawArray = ['Dave', 'Jim', 'Dave', 'Nicky', 'Nicky', 'Bob', 'Bob', 'Bob'];
Logger.log(rawArray);
// sort the array alphabetically so they are organised in the end
rawArray.sort();
// empty array for single values
var cleanArray = [];
// loop through each element of the array of duplicates *******************
rawArray.forEach(function checkDuplicates(name) {
// log current name in array of duplicates
Logger.log(name);
// perform an 'include' method check
var exists = cleanArray.includes(name);
Logger.log(exists);
// check if name exists in empty array for single values
if (!exists) {
// does not exist, push into empty array
cleanArray.push(name);
}
else {
// does already exist, do nothing
}
});
// loop through each element of the array of duplicates *******************
// log single values array
Logger.log(cleanArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment