Skip to content

Commit e9f00e0

Browse files
committed
Complete sorted union algorithm
1 parent 4489b96 commit e9f00e0

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/fcc-intermediate-algorithms/fcc-intermediate-algorithms.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ From the Freecodecamp Javascript Certification Intermediate Algorithms module
1313
- [Search and Replace](#search-and-replace)
1414
- [DNA Pairing](#dna-pairing)
1515
- [Missing Letters](#missing-letters)
16+
- [Sorted Union](#sorted-union)
1617

1718
#### Sum All Numbers In a Range
1819

@@ -198,3 +199,24 @@ export function fearNotLetter(str) {
198199
return undefined;
199200
}
200201
```
202+
203+
#### Sorted Union
204+
205+
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.
206+
207+
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
208+
209+
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
210+
211+
```javascript
212+
export function uniteUnique(...arr) {
213+
return arr.reduce((union, subArray) => {
214+
for (let num of subArray) {
215+
if (union.indexOf(num) === -1) {
216+
union.push(num);
217+
}
218+
}
219+
return union;
220+
}, []);
221+
}
222+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function uniteUnique(...arr) {
2+
return arr.reduce((union, subArray) => {
3+
for (let num of subArray) {
4+
if (union.indexOf(num) === -1) {
5+
union.push(num);
6+
}
7+
}
8+
return union;
9+
}, []);
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { uniteUnique } from "../../src/fcc-intermediate-algorithms/sorted_union";
2+
3+
test('should flatten arrays and remove duplicates', () => {
4+
expect(uniteUnique([1,2], [2, 3])).toEqual([1, 2, 3])
5+
});

0 commit comments

Comments
 (0)