Xano - Find the Gaps in an Array

Ray Deck shows how to split a flat list of integers into groups of consecutive numbers. Using a foreach loop that tracks the previous value, the code detects gaps larger than 1, closes the current group, and starts a new one. The example [1,2,3,5,6,7,8,9,11,12,13] produces three groups: [1,2,3], [5,6,7,8,9], and [11,12,13].
Chapters
Hey, John. I thought it might be easier to show than to tell how you might go about your particular problem. Now, my understanding is that your desired behavior is that the, given a list of numbers like this 1, 2, 3, and then it skips to go to 5, 6, 7, 8, 9, and then skips to go to 11, 12, 13, is you would like to be able to get the subgroups, right? The 1, 2, 3, the 5, 6, 7, 8, 9, and 11, 12, 13, all as 3 separate groups. To do this, you would, you know, we can just do it through a function just to illustrate the behavior, and you'd start with an array of numbers just like this. Great. And then we're going to need to have a few dummy variables to work with. We're going to have our output, which is going to be that list of arrays, right? It'll be a list of that will contain 1, 2, 3. Another member will be 4, 5, 6, 7, 8, 9. Another member will be 11, 12, 13. You want to have a way of storing what's going to go into them. Like, this will eventually be a 1, 2, 3. It'll be a 5, 6, 7, 8, 9, and it'll be 11, 12, 13. And then we're going to need to keep track, again this is the key thing, of what the last number was in the array, and that's really the behavior this whole thing is going to illustrate. It's being able to compare the present value in an array with the previous value. So to do this we use a foreach loop. We're going to loop over all of our numbers and we're going to use them as an instance, as an iterator variable of number, and we're going to say that if there's a more than— there's a gap greater than 1 of— if I were to just go into here, you'll see this adding 1. So if the current number is more than last number plus 1, i.e., the gap is greater than 1, then we're going to say it's not connected. This is your idea of connectedness. And this will then trigger the then. If that gap exists, we're going to say, well, we're going to close off our previous array and we're going to reinitialize so that we can be building the next one, right? So the 1, 2, 3 will be closed off and we'll start with 5 in the next. And then regardless where these things happen, we're going to add the number to the end of array, or r in this case, like the subarray. And we're going to make sure we keep last number moving up to be the number in this loop so that when we go to the next number, we have that to compare with, right? So as we move, you know, from the 1, right, starts 1, last number is 1, going to fail this test, therefore just goes into the array, fine, last number becomes 1, then the number is 2, Fails the test again because 2 is only 1 greater than 1. And now we add the 2 to the array and now we have 1, 2 in the array. And then we go again, 3, and again, oh wait, something's different because now we have 5 instead of 3. And that 5 means we hit the then. Okay, so that means we're going to add the array that contains 1, 2, 3 into arrays and we're going to then reinitialize it so it's blank. And then we're going to add the 5 to the new array, right? And then we'll do the same thing for 6, 7, 8, 9. Oh wait, there's another gap. Another gap. And in that way we get to introduce connectedness with just a few lines of code. Now, a key thing about this kind of looping mechanism is that it leaves work to be done at the end. Like, I get to the very end of this loop and it has not remembered and I still have something sitting in array, or r potentially, and in the event I do I just make sure to go add it to the end. So this makes sure that my 11, 12, 13 makes it in. Without that I would only have 1, 2, 3, 5, 6, 7, 8, 9 because r never made it into arrays. This guarantees that it will. It also guarantees I won't get an empty array, that's what the count is for. And then I want arrays to spit out at the end. So to make all this work, I go to Run Debug, illustrate, yep, 1, 2, 3, gap, 5, 6, 7, 8, 9, gap, 11, 12, 13. I should get 3 groups coming out of this. I press Run, and that's exactly what I get.










