8000 [ADD] sorted union added · luisprooc/js-algorithms@08847eb · GitHub
[go: up one dir, main page]

Skip to content

Commit 08847eb

Browse files
committed
[ADD] sorted union added
1 parent a12f0ce commit 08847eb

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,21 @@ pairElement("TTGAG") should return [["T","A"],["T","A"],["G","C"],["A","T"],["G"
482482
pairElement("CTCTA") should return [["C","G"],["T","A"],["C","G"],["T","A"],["A","T"]].
483483

484484
```
485+
486+
487+
## Sorted Union
488+
489+
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.
490+
491+
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
492+
493+
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
494+
495+
```javascript
496+
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) should return [1, 3, 2, 5, 4].
497+
498+
uniteUnique([1, 2, 3], [5, 2, 1]) should return [1, 2, 3, 5].
499+
500+
uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) should return [1, 2, 3, 5, 4, 6, 7, 8].
501+
502+
```

src/sortedUnion.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function uniteUnique(...args) {
2+
const uniteArray = args[0];
3+
args = args.splice(1);
4+
for(let i = 0; i < args.length; ++i){
5+
for(let j = 0; j < args[i].length; ++j){
6+
if(!uniteArray.includes(args[i][j])){
7+
uniteArray.push(args[i][j]);
8+
}
9+
}
10+
}
11+
12+
return uniteArray;
13+
}
14+
15+
console.log(uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]));

0 commit comments

Comments
 (0)
0