-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicking_numbers.js
More file actions
38 lines (31 loc) · 923 Bytes
/
picking_numbers.js
File metadata and controls
38 lines (31 loc) · 923 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
29
30
31
32
33
34
35
36
37
38
/*
* Complete the 'pickingNumbers' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY a as parameter.
*/
function pickingNumbers(a = []) {
const groups = a.sort().reduce((acc, curr, index) => {
const next = a[(index + 1) % a.length];
if (Math.abs(curr - next) <= 1) {
acc.push([curr, next]);
}
return acc;
}, []);
const count = {};
a.forEach((el) => {
count[el] = count[el] ? count[el] + 1 : 1;
});
return groups.reduce((acc, curr) => {
const [a, b] = curr;
const sum = a != b ? count[a] + count[b] : count[a];
// console.log(`count of a=${a} and b=${b} is `, sum);
if ( sum > acc) {
return sum;
}
return acc;
}, 0);
}
const a = [4, 6, 5, 3, 3, 1];
const b = [1, 2, 2, 3, 1, 2];
console.log(pickingNumbers(b));