 Hi guys. So in this video, we're going to be taking a look at a method that involves generating a subset of an array, and the method we're going to be looking at is called return odd sub array. It has one parameter, an integer array data, and we want to return a sub array containing all of the items in data that are odd. So how can we go about approaching this problem? Well, we can kind of separate it into three distinct steps. So the first step is we're going to need to calculate the length of the array that we're going to return. So calculate length of new array. Then in our second step, we can create the new array. And then finally, we need to fill new array with odd values from data. All right, and then what are each of these steps going to look like? Well, we can determine the length of our new array by figuring out how many items in data are odd. And we can do this by going through each item in data using a loop. And if that item is odd, we can add it to a counter for the length of our new array. So this will look like so. So we can call our counter n and we'll set n equal to zero at first. We'll then go through each item in data and we'll say if current item is odd, increase n by one. And we'll just keep doing that until we get to the end of our array. So we'll complete our array, we'll exit the loop. And then at the end, n will equal how many items in data are odd, which will be the length of our new array. Now the next step, creating a new array, this is very simple. We'll just initialize a new array which we can call result that has a length equal to n. And then how do we go about filling the new array with all of the odd values from data? Well, we can do that again using a loop. So if we have a loop like this that's going to be going through each item in data one by one, determining if it's odd, and if it is odd, adding it to our new array, then we're also going to need a counter to keep track of where we are in our new array in order to determine where we're adding each odd item from data. So we'll go ahead and call that counter K. We'll set K to zero to start with. We're then going to once again, go through each item in data just like we did above. And once again, if current item is odd, I just realized I wrote in up there is odd. So if current item is odd, then we're going to add current item to new array at index K. And then now that we've added an item in index K, we need to increase K by one. We'll keep going through our array data. If the current item is odd, we're going to add it to our new array, keep increasing the index counter of our new array by one if we add an item to it. Eventually, we will reach the end of data. We'll exit the loop. And then at the very end, we can return new array. So if anything in there is a little bit confusing, hopefully it will become nice and clear once we write it out now in Java. So using this suitor code as our guide, what's the first step? The first step is we're going to create a variable n. n equals zero. We're then going to use a loop to go through the array data. So for int i equals zero, i is less than data dot length i plus plus if current item. So in other words, if just move this down. In other words, if data i is odd, so if data i modular two does not equal zero, then we're going to increase n by one. So now we can go ahead and create our new array. And the way we can do that is we can just say, we'll call it result. So we'll say integer array result equals near integer array with a length of n. So that's it. And then even if we haven't found just as a side nerd, even if there aren't any odd values inside data, that's fine. We can still go ahead and do it just like this. n will equal zero. And this statement right here will just create an empty array. So that's fine. Now for our final step, filling the new array with the odd values from data. So just pop this, you can see. So all right, we've got our variable k, which is going to keep track of the current index we're at within result. So we'll go ahead and say int k equals zero. Once again, we're going to loop through each item in data. So same thing. I listen data dot length plus plus. And same thing if data I modular to does not equal zero. So in other words, if the value at data I is odd, then we are going to add the current item in data to the new array. k result at index k. And we do that like so. We just say result k equals data I. So the item at index k in result equals the item at index I in data. Now we've filled up index k, we're going to increase k by one. We're going to keep doing that. So essentially we will reach the end of the array data will be finished filling up result. We can X the loop and at the very end we can return result. All right. So I've written some J unit test down here below. So let's just go ahead and run this and make sure everything passes. And yeah, so that worked fine. All right. So that was just a quick example of an array problem that involves generating a subset of a larger array. And this is just a structure that can be tailored to a variety of different problems. For example, you might be asked to return a subset of an array data containing all of the even values or all of the negative values. And the general structure is going to be exactly the same. The main thing you're going to be changing is the body of the if statement here. For example, if I was asked to let's say we call this return positive sub array. And we want to return a sub array containing all of the positive items in data. The structure would pretty much be the exact same. The only thing we would be changing is instead of asking if data I is odd, we would be saying if data I is greater than 0. And then we'd be doing the same thing down here. But otherwise, it would be the exact same. So yeah, that's just a really good structure to bear in mind whenever you are asked to solve a method that involves returning a sub array. So I hope you found this video helpful. Thank you for watching and have a great day. Bye.