Published
Edited
Apr 26, 2019
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const array1 = [1,0,2,3,4];
const array2 = [3,5,6,7,8,13];
return sum(array1, array2);
function sum(arr1, arr2) {
const arrs = [arr1, arr2].sort((a, b) => a.length < b.length);
return arrs[0].reduce((res, n, i) => {
res[i] = n + (arrs[1][i] || 0);
return res;
}, []);
}
}
Insert cell
Insert cell
Insert cell
{
const a = [1, [2], [3, [[4]]],[5,6]];
return [
flatten(a),
flatten(a, true)
];
function flatten(arr, shallow) {
return shallow ?
[].concat(...arr) :
arr.reduce((res, a) => Array.isArray(a) ? res.concat(...flatten(a)) : res.concat(a), []);
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const a = [1, 3, 6, 4, 2, 1];
const b = [1, 2, 3];
const c = [-1, -3];
return [solution(a), solution(b), solution(c)];

function solution(arr) {
return Math.max(getSmallestPositive(arr.sort()), 1)
}
function solution1(arr) {

const sorted = arr.reduce((res, n, i) => {
for (let j = 0; j < res.length; j++) {
if (res[i] < res[j]) {
const x = res[j];
res[j] = res[i];
res[i] = x;
}
}
return res;
}, arr.slice());

return Math.max(getSmallestPositive(sorted), 1);
}

function getSmallestPositive(arr) {
const lastIdx = arr.length - 1;
for (let i = 1; i <= arr[lastIdx]; i++) {
if(!arr.includes(i)) {
return i;
}
}
return arr[lastIdx] + 1;

}
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more