Skip to content

Instantly share code, notes, and snippets.

@jasonroelofs
Forked from mattslack/dupefinder.js
Last active December 27, 2017 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonroelofs/526b17feaafede5222d1 to your computer and use it in GitHub Desktop.
Save jasonroelofs/526b17feaafede5222d1 to your computer and use it in GitHub Desktop.
Find duplicate IDs in a document.
(function () {
"use strict";
var find_dupes = function () {
var identified = document.querySelectorAll('[id]'),
found = {},
id,
idx;
for (idx = 0; idx < identified.length; idx++) {
id = (identified[idx]).getAttribute('id');
if(found[id]) {
found[id] += 1;
} else {
found[id] = 1;
}
}
var dupes = [];
for (id in found) {
if(found[id] > 1) {
dupes.push(id);
}
}
if (dupes.length) {
return "Duplicate IDs:" + dupes.join(", ");
}
return "No duplicate IDs";
};
console.log(find_dupes());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment