I have an array (or Set?) of arr = ['a', 'b', 'c']
and I want to add d
to it, which could be done with arr.push('d')
.
But I only want unique values in the array, and I want the latest values added to be in the front of the array.
So if I first add d
the array should become ['d', 'a', 'b', 'c']
and if I now add b
the array should become ['b', 'd', 'a', 'c']
etc.
Should it be something like
function addElement(arr, element) {
if (arr.includes(element)) {
arr.splice(arr.indexOf(element, 1));
}
arr.unshift(element);
}
I guess this could be done with Sets, since sets can only contain unique values.