Find the first missing number in a sequence of numbers

I am trying to figure out how to find the first missing number of numbers like this (1,2,3,5,6,9,10,15)

I want to put the first missing number, # 4, into a variable for later use, but don’t know how to do it?

I tried this, but it only gives me the last number:

var mynumbers=new Array(1,2,3,6,9,10); for(var i = 1; i < 32; i++) { if(mynumbers[i] - mynumbers[i-1] != 1) { alert("First missing number id: "+mynumbers[i]); break; } } 

First of all, it gives me the first number after the β€œhole” in the numbers, and secondly, it continues to warn all the numbers that appear after the β€œhole” if I do not insert a gap. I only need the first missing number of numbers from 1 to 32. How do I do this?

I hope for help and thanks in advance; -)

+7
javascript numbers sequence
source share
7 answers

How about this

 var mynumbers = new Array(1,2,3,6,9,10); var missing; for(var i=1;i<=32;i++) { if(mynumbers[i-1] != i){ missing = i; alert(missing); break; } } 
+10
source share

O (n) solutions are easy , but this is a common question for the interview and often we are looking for O (log n) solution . Here is the javascript code. This is basically a modified binary search.

 function misingNumInSeq(source, min = 0, max = source.length - 1){ if(min >= max){ return min + 1; } let pivot = Math.floor((min + max)/2); // problem is in right side. Only look at right sub array if(source[pivot] === pivot + 1){ return misingNumInSeq(source, pivot + 1, max); } else { return misingNumInSeq(source, min , pivot); } } 

Exit

 misingNumInSeq([1,2,3,5,6,9,10,15]) 4 
+5
source share

Under if(mynumbers[i] - mynumbers[i-1] != 1) you want to say that the series will always increase by 1 ?

 var missing = (function (arr) { var i; for (i = 0; i < arr.length; ++i) { if (i + arr[0] !== arr[i]) return i + arr[0]; } if (i < 32) // if none missing inside array and not yet 32nd return i + arr[0]; // return next }([1,2,3,6,9,10])); // 4 alert(missing); 
+3
source share

You need a break, no matter what. What is that there; so that the cycle does not continue to the end. And you should use the length of the array instead of hardcoding 32 as the final condition, because your numbers increase to 32, but there may be holes in the list, so there won't be 32 elements in the array.

Since you know that each element must be 1 more than the previous element, the number in the hole is clearly mynumbers[i - 1] + 1 .

 var mynumbers = new Array(1,2,3,6,9,10); for(var i = 1; i < mynumbers.length; i++) { if(mynumbers[i] - mynumbers[i-1] != 1) { alert("First missing number id: " + (mynumbers[i - 1] + 1)); break; } } 

EDIT: This is only true if the missing number is not 1. To catch this, you will need to check if (mynumbers[0] != 1)

+1
source share

Edit:

 function findFirstMissing(array) { for (var i = 0; i < array.length; i++) { if (i+1 !== array[i]) { return i+1; } } } 

 function findFirstMissing(array) { for (var i = 0; i < array.length; i++) { if (array[i+1] - array[i] !== 1) { return array[i] + 1; } } } 

If you do it this way, saving it in a variable is easy:

 var missing = findFirstMissing(array); 
+1
source share
 const firstNonConsecutive = arr => arr.find((el, i, arr) => (arr[i] - arr[i-1]) !== 1 && i !== 0) 

this solution works for an array of positive numbers.

+1
source share
 for(var i = 1; i < mynumbers.length; i++) { if(mynumbers[i] - mynumbers[i-1] != 1) { alert("First missing number id: "+mynumbers[i-1]+1); i = mynumbers.length; // Replace the break } } 

If you want, you can add an initial check: if (mynumbers[0] != 1) { ... }

-one
source share

All Articles