JAVASCRIPT
javascript check if array has duplicates
function hasDuplicates(array) {
return (new Set(array)).size !== array.length;
}
check for duplicates in array javascript
[1, 2, 3].every((e, i, a) => a.indexOf(e) === i) // true
[1, 2, 1].every((e, i, a) => a.indexOf(e) === i) // false
Find duplicate or repeat elements in js array
function findUniq(arr) {
return arr.find(n => arr.indexOf(n) === arr.lastIndexOf(n));
}
console.log(findUniq([ 0, 1, 0 ]))
console.log(findUniq([ 1, 1, 1, 2, 1, 1 ]))
console.log(findUniq([ 3, 10, 3, 3, 3 ]))
console.log(findUniq([ 7, 7, 7, 20, 7, 7, 7 ]))
javascript check for duplicates in array
function checkIfDuplicateExists(w){
return new Set(w).size !== w.length
}
console.log(
checkIfDuplicateExists(["a", "b", "c", "a"])
// true
);
console.log(
checkIfDuplicateExists(["a", "b", "c"]))
//false
how to count duplicates in an array javascript
arr.reduce((b,c)=>((b[b.findIndex(d=>d.el===c)]||b[b.push({el:c,count:0})-1]).count++,b),[]);
count duplicate array javascript
const months = ['april','may','june','may','may','june'];
const countDuplicates = months.reduce((obj,month)=>{
if(obj[month] == undefined){
obj[month] = 1;
return obj;
}else{
obj[month]++;
return obj;
}
},{});
console.log(countDuplicates);//output:{april: 1, may: 3, june: 2}