Created
September 16, 2014 03:06
-
-
Save loonison123/d87e085cb3b7f0f02dcd to your computer and use it in GitHub Desktop.
Find dupes in JS array no comments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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