javascript array check duplicate values code example
Example 1: how to check for duplicate syntax in javascript
let strArray = [ "q", "w", "w", "w", "e", "i", "u", "r"];
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
console.log(findDuplicates(strArray))
console.log([...new Set(findDuplicates(strArray))])
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: js find duplicates in array
[1, 2, 2, 4, 3, 4].filter((e, i, a) => a.indexOf(e) !== i)