1

Write a method deleteElement which takes as input an int[] and an int target and deletes all occurrences of target from the array. The method should return the newint[] . Question to consider: Why is it that we have to return an array and can't simply change the input parameter array?

public class warm5{
  public static void main(String[] args){
    int[] array1= {1,2,2,3,4,5,2};
    int target1 = 2;
    deleteElement(array1,target1);

    public static int[] deleteElement(int[] array, int target){
      for(int i = 0, i<array.length, i++){
        if(array1[i] == target){

        }
      }
    }
  }
}

Here is what i wrote, im not sure how to continue it to remove the 2's in the array. please help!

4
  • 2
    Why do you have a method deleteElement inside of your main method? Commented Oct 29, 2013 at 19:41
  • Apart from above comment, don't simply add it to new array if element == 2 Commented Oct 29, 2013 at 19:42
  • well i wanted to use it for a system.out.println for checking my code Commented Oct 29, 2013 at 19:44
  • @RoddyoftheFrozenPeas i got what u mean there, its my bad, i forgot about the bracket Commented Oct 29, 2013 at 19:59

3 Answers 3

4

You can't delete elements from an array, by definition they're of fixed size. What you can do is create a new array, copy all the elements from the old array except the ones that you intend to delete and return the new array. Or, use an ArrayList, which has operations that allow removing elements:

Sign up to request clarification or add additional context in comments.

Comments

1

First, iterate through your array and figure out how many of your target element are present.

Once you know that, you know the size of your new array. As Oscar mentions, you can't "remove" from an array, you just make a new array without the elements you don't want in it.

int targetCount = 0;

for (int i = 0; i < array.length; i++) {
    if (array[i] == target) {
        targetCount++;
    }
}

Now you know how many items will be in your new array: array.length-targetCount.

int[] newArray = new int[array.length-targetCount];
int newArrayIdx = 0;
for (int i = 0; i < array.length; i++) {
    if (array[i] != target) {
        newArray[newArrayIdx] = target;
        newArrayIdx++;
    }
}

Here we iterate through the old array and check each element to see if it's our target. If it's not, we add it to the new array. We have to keep track of our old array's index and our new array's index independently or we may risk trying to assign an index outside of the array bounds.

2 Comments

can you explain to me what does newArrayIdx means please?
newArrayIdx is the current index of the new array. You can't just use i because that's the current index of the old array, and you already know that your new array will have a fewer items in it than your old one. There are other ways of determining the index of the new array, but I thought this one the simplest to show and explain.
0

This is a common interview question. In the solution presented by Oscar you do not know what will be the size of the new array. So the solution does not work or is memory inefficient.

Trick is to loop over the array and at any time when you encounter an element equal to the given element you put that element towards the end of the array and swap it with the element that was there at the end position. By doing this you are collecting all occurrences of given element at the end of the array.

Here is a working logic

deleteElement(int[] given, int elem) {

  int endIdx = array.length - 1;
  for(int idx = 0; idx <= endIdx; idx++) {
    if(given[idx] == elem) {
      //swap idx with endIdx
      int tmp = given[endIdx];
      given[endIdx] = given[idx];
      given[idx] = tmp;
      endIdx--;
    }
  }

  return Arrays.copyOfRange(given, 0, endIdx);

}

2 Comments

this doesn't preserve the ordering of the other elements, which may or may not be a requirement.
As per question, it isnt :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.