Reputation: 533
In an array that has duplicates, I am trying to remove the first instance of duplicate from it and wanted to only use second instance of that duplicate in it. say I have this array
const a = ['a', 'b', 'c', 'd', 'a', 'b']
when I do this const b = [...new Set(a)]
my output will be ['a', 'b', 'c', 'd']
But I need the order/output to be ['c', 'd', 'a', 'b']
is there a why I can achieve this?
Upvotes: 0
Views: 56
Reputation: 17654
a quick way to do this is to reverse the array, remove the duplicates, then reverse it back again :
const a = ['a', 'b', 'c', 'd', 'a', 'b']
const b = [...new Set(a.reverse())].reverse()
console.log(b);
Upvotes: 3