Another way would be to take the members of the input array which you want to keep, and copy only them to a new array. Try writing that code and then see if you think it's better.
By the way the code you posted puts zeroes into the result. Is that allowed? Shouldn't the amended array be shorter? I have to mention this, your problem statement doesn't say whether you're allowed to return an array which has numbers which weren't in the input array. (Besides which you appear to assume that zero isn't going to be in the input array.)
Dan Foster wrote:Is there a better way to do it? Please suggest
Well, there is what Paul suggested. However, if you want to do this removal in place, then my advice would be to keep track of how many elements have been "removed" and shift all the others according to that value. That way you only move those elements that need moving, and you only move them once.
Your current code shifts all remaining elements (which might also include ones you want to remove) 1 position left - ie, it works rather like ArrayList.remove(int).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Create Edit Print & Convert PDF Using Free API with Java