Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea75439

Browse files
committed
Combining Arrays Without Duplicates
1 parent e2f29e2 commit ea75439

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ Once inside, run the command below:
1717
8. Search and Replace
1818
9. Anagram
1919
10. Pig-latin
20-
11. Chunk Array
20+
11. Chunk Array
21+
12. Combining Arrays Without Duplicates

‎src/mergeArray/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// pick a solution and insert here to run the test.
2+
3+
function mergeArrays(...arrays) {
4+
let jointArray = []
5+
6+
arrays.forEach(array => {
7+
jointArray = [...jointArray, ...array]
8+
});
9+
return Array.from(new Set([...jointArray]))
10+
}
11+
12+
module.exports = mergeArrays;

‎src/mergeArray/solutions.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// USING A SET
2+
function mergeArrays(...arrays) {
3+
4+
let jointArray = []
5+
6+
arrays.forEach(array => {
7+
jointArray = [...jointArray, ...array]
8+
});
9+
10+
return [...new Set([...jointArray])]
11+
12+
}
13+
14+
// USING ARRAY.FROM() WITH SET
15+
16+
function mergeArrays(...arrays) {
17+
let jointArray = []
18+
19+
arrays.forEach(array => {
20+
jointArray = [...jointArray, ...array]
21+
});
22+
return Array.from(new Set([...jointArray]))
23+
}
24+
25+
// USING .FILTER()
26+
27+
function mergeArrays(...arrays) {
28+
29+
let jointArray = []
30+
31+
arrays.forEach(array => {
32+
jointArray = [...jointArray, ...array]
33+
})
34+
35+
const uniqueArray = jointArray.filter((item,index) => jointArray.indexOf(item) === index)
36+
37+
return uniqueArray
38+
}
39+
40+
// USING .REDUCE()
41+
42+
function mergeArrays(...arrays) {
43+
44+
let jointArray = []
45+
46+
arrays.forEach(array => {
47+
jointArray = [...jointArray, ...array]
48+
})
49+
50+
const uniqueArray = jointArray.reduce((newArray, item) =>{
51+
if (newArray.includes(item)){
52+
return newArray
53+
} else {
54+
return [...newArray, item]
55+
}
56+
}, [])
57+
58+
return uniqueArray
59+
}

‎src/mergeArray/test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const mergeArrays = require('./index');
2+
3+
test('mergeArrays is a function', () => {
4+
expect(typeof mergeArrays).toEqual('function');
5+
});
6+
7+
test('Combines 5 arrays of numbers without dubplicates', () => {
8+
expect(mergeArrays([1,2],[2,3],[3,4],[4,5])).toEqual([1,2,3,4,5]);
9+
});
10+
11+
test('Combines 3 arrays of strings without dubplicates', () => {
12+
expect(mergeArrays(['a','b','z'],['m','n','a'],['z','y'])).toEqual(['a','b', 'z', 'm', 'n', 'y']);
13+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /