I am looking for a way to create a new array that contains only unique values from both the first and second arrays, maintaining their original order.
This is my current approach:
function union(first, second) {
return first.filter(function(value) {
return second.indexOf(value) === -1;
})
.concat(second.filter(function(value){
return first.indexOf(value) === -1;
}))
}
The expected output should be as follows:
union([2, 1], [2, 3]);
// -> [2, 1, 3]
union(['html', 'css', 'javascript'], ['php', 'css', 'sql']);
// -> ["html", "css", "javascript", "php", "sql"]
union(
['a', 'link', 'to', 'the', 'past'],
['the', 'adventure', 'of', 'link']
)