OneLazy OneLazy - 14 days ago 6
Javascript Question

JS: remove duplicate values in array, including the original

Does anyone know how to remove duplicates in an array including the original value? I came across different snippets like this
and this and some others but none in particular is removing the original node at the same time. Care to share a little snippet ? TIA!

Example:

[1, 1, 2, 3, 5, 5] -> [2, 3]

Answer

You could try filtering your array based on the number of occurrences of a specific value in that array:

var arr = [1, 1, 2, 3, 5, 5]

var res = arr.filter((el, _, arr) => {
      return arr.filter(el2 => el2 === el).length === 1
})

console.log(res)

Comments