ttomasko
Programmer
- Aug 6, 2008
- 28
Hello,
I have been tearing my hair out on how to solve this. I will have an array of numbers. I'll never know the starting and ending numbers but they will be in order. However, there may be gaps in the order. It is the gaps I am interested in.
Let's say I start with the following array:
var a = [11,12,13,15,16,19,20]; //missing 14, 17, 18
I figured that the best thing would be to get an array of all whole numbers that start and end with the start and end numbers in "a." Here is how I create that array:
var aFirst = a[0];
var aLast = a[a.length-1];
//2. define length of new array
var b = 1+aLast - aFirst;
//3. following makes array of whole numbers between first and last numbers of a array
var c = [];
for(var i = 0; b > i; i++){
c.push(aFirst +i);
}//end for
//c = [11,12,13,14,15,16,17,18,19,20]
Now what stumps me is the next thing to do: how to compare those two arrays and make an array of the missing numbers.
Or maybe there is a simpler way to find the gaps in the first array without having to create a second one.
Thanks,
Tom
I have been tearing my hair out on how to solve this. I will have an array of numbers. I'll never know the starting and ending numbers but they will be in order. However, there may be gaps in the order. It is the gaps I am interested in.
Let's say I start with the following array:
var a = [11,12,13,15,16,19,20]; //missing 14, 17, 18
I figured that the best thing would be to get an array of all whole numbers that start and end with the start and end numbers in "a." Here is how I create that array:
var aFirst = a[0];
var aLast = a[a.length-1];
//2. define length of new array
var b = 1+aLast - aFirst;
//3. following makes array of whole numbers between first and last numbers of a array
var c = [];
for(var i = 0; b > i; i++){
c.push(aFirst +i);
}//end for
//c = [11,12,13,14,15,16,17,18,19,20]
Now what stumps me is the next thing to do: how to compare those two arrays and make an array of the missing numbers.
Or maybe there is a simpler way to find the gaps in the first array without having to create a second one.
Thanks,
Tom