Skip to content

Instantly share code, notes, and snippets.

@dillansimmons
Last active October 18, 2016 18:21
Show Gist options
  • Save dillansimmons/d4a6f432d84183264f37342ac54ee0ca to your computer and use it in GitHub Desktop.
Save dillansimmons/d4a6f432d84183264f37342ac54ee0ca to your computer and use it in GitHub Desktop.
Find duplicates in single array
//Array
var y = ["?","X","Z","Q","W","W"];
/*OPTION ONE*/
var find_dupe = function (array) {
/* Map Arrays, make uppercase if wanted
var yy = array.map(function(x){ return x.toUpperCase() })*/
// Sort function
var sorted_arr = array.slice().sort(); // We use slice to clone the array so the original array won't be modified)
var results = [];
for (var i = 0; i < array.length -1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results
}
//Print Duplicates: in this case [W] *If more than 2 will list dupes multiple times
console.log(find_dupe(y));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment