This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Devaka Cooray
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Junilu Lacar
• Paul Clapham
• Knute Snortum
Saloon Keepers:
• Ron McLeod
• Tim Moores
• Stephan van Hulst
• salvin francis
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# A Better way of doing this

Greenhorn
Posts: 23
Question - Given an specific integer and an array of integers, find and remove all occurrences of the given integer and return an amended array.

I solved it. Here is my solution -

Is there a better way to do it? Please suggest

author & internet detective
Posts: 39129
723
• 1
Dan,
That looks fine. You might want to skip outputting the 0 elements at the end.

In the real world, the better way of doing this is to use an ArrayList. But you are understandably not allowed to do that in the assignment.

Sheriff
Posts: 24089
54
• 1
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.)

Bartender
Posts: 1093
36
My humble proposal...

Dan Foster
Greenhorn
Posts: 23
@Paul - What can i move instead of 0 in the vacant places? I cannot shrink an existing array.

But in that way i will have to use another array in which I move the rest items since i cant shorten an already existing array. Cant it be done using a single array?

Bartender
Posts: 10750
68

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).

Winston

 All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad: RavenDB is an Open Source NoSQL Database thatâ€™s fully transactional (ACID) across your database https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database