Skip to content

Instantly share code, notes, and snippets.

@loonison123
Created September 16, 2014 03:06
Show Gist options
  • Select an option

  • Save loonison123/d87e085cb3b7f0f02dcd to your computer and use it in GitHub Desktop.

Select an option

Save loonison123/d87e085cb3b7f0f02dcd to your computer and use it in GitHub Desktop.
Find dupes in JS array no comments
var cached = {};
var allTags = ["red", "pants", "jeans", "red", "black", "jeans", "black", "pants", "jeans"];
var duplicatedTags = [];
for (var i=0;i<allTags.length; i++) {
if (cached[allTags[i]] == undefined) {
cached[allTags[i]] = allTags[i];
if (hasDup(allTags, allTags[i], allTags.length)) {
duplicatedTags.push(allTags[i]);
}
}
}
// duplicatedTags now contains ["jeans"]
// You know that the 3 images you selected to edit tags all contain 'jeans' as a tag
console.log(duplicatedTags);
function hasDup(arr, val, greaterThan) {
var count = 0;
for (var i = 0; i < arr.length; i++) {
if (arr[i] == val)
count++;
}
if (count >= greaterThan){
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment