1
+ /*
2
+ Sorted Union:
3
+ 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.
4
+ In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
5
+ The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
6
+ Check the assertion tests for examples.
7
+
8
+ - uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) should return [1, 3, 2, 5, 4].
9
+ - uniteUnique([1, 2, 3], [5, 2, 1]) should return [1, 2, 3, 5].
10
+ - uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) should return [1, 2, 3, 5, 4, 6, 7, 8].
11
+ */
12
+ function uniteUnique ( arr ) {
13
+ const args = [ ...arguments ] ;
14
+ const output = [ ] ;
15
+
16
+ for ( const innerArray of args ) {
17
+ for ( const item of innerArray ) {
18
+ if ( ! output . includes ( item ) ) {
19
+ output . push ( item ) ;
20
+ }
21
+ }
22
+ }
23
+
24
+ return output ;
25
+ }
26
+
27
+ console . log ( uniteUnique ( [ 1 , 3 , 2 ] , [ 5 , 2 , 1 , 4 ] , [ 2 , 1 ] ) ) ;
28
+ console . log ( uniteUnique ( [ 1 , 2 , 3 ] , [ 5 , 2 , 1 ] ) ) ;
29
+ console . log ( uniteUnique ( [ 1 , 2 , 3 ] , [ 5 , 2 , 1 , 4 ] , [ 2 , 1 ] , [ 6 , 7 , 8 ] ) ) ;
0 commit comments