sai
sai

Reputation: 533

Remove duplicates in array where first instance should be ignored and only use second instance

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

Answers (1)

Taki
Taki

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

Related Questions