DEV Community

Cover image for Day 11 of JavaScriptmas - Avoid Obstacles Solution
Sekti Wicaksono
Sekti Wicaksono

Posted on

Day 11 of JavaScriptmas - Avoid Obstacles Solution

Day 11 is finding the minimum missing number from the sequence of number

For instance, an array with [5,3,6,7,9].
So, the minimum missing number will be 4.

This is JavaScript solution

function avoidObstacles(nums) { // sorted input let sortedNums = nums.sort(); // create sequence number from sortedNums value (min to max) let sequenceNum = []; for(let i = sortedNums[0]; i <= sortedNums[sortedNums.length - 1]; i++) { sequenceNum.push(i); } // find intersection that returns unmatch number between 2 arrays let intersection = sequenceNum.filter(el => sortedNums.indexOf(el) == -1); // find the minimum value of intersection let min = Math.min(...intersection); return min; } 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)