DEV Community

Discussion on: Find Duplicates in an Array

Collapse
jonrandy profile image
Jon Randy • Edited on

Shorter version of 2, using reduce:

const findDupes = a=>a.reduce(([u,d],x)=>u.includes(x)?[u,[...d,x]]:[[...u,x],d],[[],[]])[1]

Or a vastly faster Set based version:

const findDupes = a=>[...a.reduce(([u,d],x)=>u.has(x)?[u,d.add(x)]:[u.add(x),d],[new Set(),new Set()])[1]]