0

I have to create a method that takes in an array and value as arguments and returns a new array with the specified value removed.

Here's my attempt:

public static int[] remove(int[] nums,int value){
    int[] after = new int[nums.length-1];
    for (int i = 0; i < nums.length; i++) {
        if (!(nums[i] == value)) {
            after[i] = nums[i];
        }
    }
    nums = after;
    return nums;
}

The code throws

ArrayIndexOutOfBoundsException

bur I don't know why.

3
  • 5
    what is your question? Does the above code not work? If so, how does it fail? Commented Feb 10, 2018 at 0:35
  • 1
    If value is not the last element in the nums array you'll get a ArrayIndexOutOfBoundsException since after length is 1 less then num length and i goes as high as nums.length - 1. Commented Feb 10, 2018 at 0:35
  • As KyrSt says, you need to add error messages so that this question is helpful to other programmers. What happens when you run it? Commented Feb 10, 2018 at 0:38

3 Answers 3

1

First you need to find how big the new array is, after you can populate the new array without the value to remove. There are better ways to do this, but this is to get you started.

public static int[] remove (int[] nums, int value)
{
    int[] after;

    //find out the length of the array
    int count = 0;
    for (int num : nums)
    {
        if (num != value)
        {
            count++;
        }
    }

    after = new int[count];

    //add all the elements except the remove value
    int i = 0;
    for (int num : nums)
    {
        if(num != value)
        {
            after[i] = num;
            i++;
        }
    }

    return after;
}
Sign up to request clarification or add additional context in comments.

Comments

1

KISS. It's a 1-liner:

public static int[] remove(int[] nums, int value){
    return IntStream.stream(nums).filter(i -> i != value).toArray();
}

Comments

0

Code is trying to copy values from original array to new array leaving a blank or space(value 0) in place of targeted value. The error ArrayIndexOutOfBoundsException is showing because you are initialising the after with nums.lenth-1 int[] after = new int[nums.length-1]; You can change that or swap the targeted element with the last element and copy entire array except the last.

public static int[] remove (int[] nums,int value){
    int[] after = new int[nums.length-1];

    for(int i = 0; i < nums.length; i++) {
        if((nums[i] == value)) {
            //changing the target value with the last value of nums array
            nums[i]=nums[nums.length-1];
            nums[nums.length-1]=value;
        }
    }

    //Copying the entire array except the last
    for(int i=0;i<nums.length-1;i++){
        after[i]=nums[i];
    }
    return after;
}

And I am assuming that there is only one target element

Comments

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.