Example 1: 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 2: js find duplicates in array
const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
const count = names =>
names.reduce((a, b) => ({ ...a,
[b]: (a[b] || 0) + 1
}), {})
const duplicates = dict =>
Object.keys(dict).filter((a) => dict[a] > 1)
console.log(count(names))
console.log(duplicates(count(names)))
Example 3: javascript duplicate an array
let arr =["a","b","c"];
const duplicate = [...arr];
const duplicate = Array.from(arr);
Example 4: check for duplicates in array javascript
[1, 2, 3].every((e, i, a) => a.indexOf(e) === i)
[1, 2, 1].every((e, i, a) => a.indexOf(e) === i)
Example 5: find duplicate values in array javascript
const findDuplicates = (arr) => {
let sorted_arr = arr.slice().sort();
let results = [];
for (let i = 0; i < sorted_arr.length - 1; i++) {
if (sorted_arr[i + 1] == sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
let duplicatedArray = [9, 4, 111, 2, 3, 9, 4, 5, 7];
console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
Example 6: js find duplicates in array
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i)