dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
85 lines
1.9 KiB
Markdown
85 lines
1.9 KiB
Markdown
---
|
|
id: a105e963526e7de52b219be9
|
|
title: Sorted Union
|
|
challengeType: 1
|
|
forumTopicId: 16077
|
|
dashedName: sorted-union
|
|
---
|
|
|
|
# --description--
|
|
|
|
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
|
|
|
|
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
|
|
|
|
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
|
|
|
|
Check the assertion tests for examples.
|
|
|
|
# --hints--
|
|
|
|
`uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1])` should return `[1, 3, 2, 5, 4]`.
|
|
|
|
```js
|
|
assert.deepEqual(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]), [1, 3, 2, 5, 4]);
|
|
```
|
|
|
|
`uniteUnique([1, 2, 3], [5, 2, 1])` should return `[1, 2, 3, 5]`.
|
|
|
|
```js
|
|
assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1]), [1, 2, 3, 5]);
|
|
```
|
|
|
|
`uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8])` should return `[1, 2, 3, 5, 4, 6, 7, 8]`.
|
|
|
|
```js
|
|
assert.deepEqual(uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]), [
|
|
1,
|
|
2,
|
|
3,
|
|
5,
|
|
4,
|
|
6,
|
|
7,
|
|
8
|
|
]);
|
|
```
|
|
|
|
`uniteUnique([1, 3, 2], [5, 4], [5, 6])` should return `[1, 3, 2, 5, 4, 6]`.
|
|
|
|
```js
|
|
assert.deepEqual(uniteUnique([1, 3, 2], [5, 4], [5, 6]), [1, 3, 2, 5, 4, 6]);
|
|
```
|
|
|
|
`uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1])` should return `[1, 3, 2, 5, 4]`.
|
|
|
|
```js
|
|
assert.deepEqual(uniteUnique([1, 3, 2, 3], [5, 2, 1, 4], [2, 1]), [1, 3, 2, 5, 4]);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function uniteUnique(arr) {
|
|
return arr;
|
|
}
|
|
|
|
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function uniteUnique(arr) {
|
|
return [].slice.call(arguments).reduce(function(a, b) {
|
|
return [].concat(
|
|
a,
|
|
b.filter(function(e, currentIndex) {
|
|
return b.indexOf(e) === currentIndex && a.indexOf(e) === -1;
|
|
}));
|
|
}, []);
|
|
}
|
|
```
|