-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayCombinations.js
More file actions
28 lines (24 loc) · 958 Bytes
/
Copy patharrayCombinations.js
File metadata and controls
28 lines (24 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Description:
// In this Kata, you will be given an array of arrays and your task will be to return the number of unique arrays that can be formed by picking exactly one element from each subarray.
// For example: solve([[1,2],[4],[5,6]]) = 4, because it results in only 4 possibilites. They are [1,4,5],[1,4,6],[2,4,5],[2,4,6].
// Make sure that you don't count duplicates; for example solve([[1,2],[4,4],[5,6,6]]) = 4, since the extra outcomes are just duplicates.
//my solution
function solve(arr) {
let answer = 1;
for (let i = 0; i < arr.length; i++) {
answer *= arr[i]
.sort()
.filter((current, index) => current != arr[i][index + 1]).length;
}
return answer;
}
//using what Leon asked
function solve(arr) {
return arr
.map((a) => {
let s = new Set();
a.forEach((el) => s.add(el));
return s.size;
})
.reduce((out, n) => out * n, 1);
}