I suggest that for each and every operation you carry out, print out what values you have before it, and then afterwards print out the thing you accomplished with that operation. You should be able to see the effects of every single step you carry out. Ideally you would do this while you write the code, so you would decide what should happen next, print out the things you need, write code for the operation, and print out the result, and check that this is really the result you wanted before you move on with the next thing whatever that is, and repeat the process for that.
You can consider what such output would look like if you manually wrote down all the actions carried out.
presumably you would visit each valid index in order, and during each such visit find out what the two values are, so you’d print those, and then you’d compare them, so you’d print out the result of the comparison, and if it is decided the index should be kept, you might print out the current list of indices, then add the new index, the print out the indices again so that you can see that it did indeed get added… then the would follow similar output for the rest of the visited indices