Example 1: javascript remove duplicate in arrays
function removeDuplicates(array) {
return array.filter((a, b) => array.indexOf(a) === b)
};
function removeDuplicates(array) {
let x = {};
array.forEach(function(i) {
if(!x[i]) {
x[i] = true
}
})
return Object.keys(x)
};
function removeDuplicates(array) {
array.splice(0, array.length, ...(new Set(array)))
};
function removeDuplicates(array) {
let a = []
array.map(x =>
if(!a.includes(x) {
a.push(x)
})
return a
};
/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"
Example 2: use set to remove duplicates in javascript
const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
let unique = [...new Set(names)];
console.log(unique);
Example 3: count duplicates array js
uniqueCount = ["a","b","c","d","d","e","a","b","c","f","g","h","h","h","e","a"];
var count = {};
uniqueCount.forEach(function(i) { count[i] = (count[i]||0) + 1;});
console.log(count);
Example 4: remove duplicates array javascript
var data = [1, 2, 'Peter', 3, 3,'Peter', 2, 2, 1, 2, 3, 4, 4, 5, 4];
var unique = data.filter((v, i, a) => a.indexOf(v) === i);
Example 5: javascript to remove duplicates from an array
uniqueArray = a.filter(function(item, pos) {
return a.indexOf(item) == pos;
})
Example 6: javascript find duplicate in array
const isThereADuplicate = function(arrayOfNumbers) {
let counts = {};
for(var i = 0; i <= arrayOfNumbers.length; i++) {
if(counts[arrayOfNumbers[i]] === undefined) {
counts[arrayOfNumbers[i]] = 1;
} else {
return true;
}
}
return false;
}