0

I am trying to create a method (without using arraylist) to return a new array that removes all instances of some integer (call it x). (For example, b=[2,5,3,2,7] b.remove(2) would return [5,3,7]. This code I have been working on (one of several hours worth of different attempts) seems to work when there is one occurence of X, but not many. When there are many, it sizes the new array correctly, but does not copy the data correctly for at/after the second occurence of X. What I am trying to do is set a counter for each time X occurs, then set a new array that has length (old array length - count variable). Then I need to shift all the data after any occurence of X left. Here's my current code:

public Sequence remove(int n) {
    int count = 0;
    int a = 0;
    for (int z=0; z < this.values.length; z++) {
        if (this.values[z] == n)
            count++;
    }

    Sequence newSequence = new Sequence(this.values.length - count);
    for (int b=0; b < this.values.length - count; b++) {
        if (this.values[a] != n) {
            newSequence.values[a] = this.values[a];
            a++;
        } else {
            newSequence.values[a]=this.values[a+1];     
        }
    }

    return newSequence;
}
2
  • Are you allowed to use anything other than array? like sets? Commented Jun 28, 2017 at 4:54
  • The problem in your code is you are using same index for your old and new sequence. if you are iterating the original array, then start iterating it with b and this.values[b] != n, then add the value in the newSequence.values[a] and increment a. you don't need a+1 logic and all. Commented Jun 28, 2017 at 5:55

2 Answers 2

3

I think the logic for populating the new resized array should be something like this:

  • walk through the entire original array
  • if a given value be the one you want removed, do nothing
  • otherwise add it to the new array and also increment the index in the new array


int pos = 0; // keeps track of position in newSequence.values

for (int i=0; i < this.values.length; i++) {
    if (this.values[i] != n) {
        newSequence.values[pos] = this.values[i];
        pos++;  
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This solution would return an array with the same size as the old one and in most cases the new array may has one or more null indices.
@STaefi No, it wouldn't. The new array is indexed by the variable pos, which could only ever reach the size of the new array which was allocated earlier in his code. Read my code again.
I can see the pos variable in your code, but you didn't provide the code for initializing the size of the new array. So did you explicitly confirm the question's code which loops over the original array to calculate the size for the new one?
@STaefi Did you miss this line of code: Sequence newSequence=new Sequence(this.values.length-count); ? The OP clearly appears to already have allocated the new array correctly.
@TimBiegeleisen thank you, this was very helpful. I think I was trying to make it overly complicated with attempting to shift all digits left, when just printing a digit if it is NOT the remove digit makes much more sense and is much less complicated.
1

To be honest I did not completely get what you trying to do. But I understood the problem and your code. I would follow these steps to solve this problem.

  • Iterate through the array and count the number of times n (assuming you want to remove n) occurs. This count is stored in count variable.
  • Create a new array with size values.length-count (here values is the array)
  • Copy numbers from values array to new array.

This gives a O(n) solution.

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.