remove duplicates in js using set code example
Example 1: javascript remove duplicate strings from array
let uniqueArray = [...new Set(arrayWithDuplicates)];
function removeArrayDuplicates(arrayWithDuplicates) {
let seen = {};
let uniqueArray = [];
let len = arrayWithDuplicates.length;
let j = 0;
for(let i = 0; i < len; i++) {
let item = arrayWithDuplicates[i];
if(seen[item] !== 1) {
seen[item] = 1;
uniqueArray[j++] = item;
}
}
return uniqueArray;
}
Example 2: removing duplicates from array javascript
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});