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]
You could use a check for index and last index.
var arr = [1, 1, 2, 3, 5, 5],
res = arr.filter((a, _, aa) => aa.indexOf(a) === aa.lastIndexOf(a));
console.log(res);
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With