duplicate value check in list js code example
Example 1: 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 2: function that duplicates data in array js
function doubleValues(array) {
var newArray = [];
array.forEach(function (el) { newArray.push(el, el); });
return newArray;
}
console.log(doubleValues([1,2,3]));