DEV Community

Discussion on: Finding a single item in an array

Collapse
 
sroehrl profile image
neoan

I am usually not a fan of ugly one liners, but if you want it concise:

const arr = [1,1,2,2,4,5,4]
const singles = [];
arr.forEach(el => arr.filter(el2 => el == el2).length == 1) ? singles.push(el) : null)

Enter fullscreen mode Exit fullscreen mode