1

I am doing a simple Java program and I need to remove all of the contiguous duplicates in a String ArrayList.

My String ArrayList is something like this:

list = [a,b,c,c,d,a,b,c,d]

My goal is removing all (and only!) the contiguous duplicates so that the result would be: [a,b,c,d,a,b,c,d]. As you can see, one of the two contiguous "c" has been removed.

I tried something like this:

for (int i = 0; i<list.size(); i++) {

        if (list.get(i).compareTo(list.get(i+1))==0) {
            positionToRemove.add(i);
        }

    }

Where positionToRemove will at the end contain all the position of the contiguous elements which I will then remove using list.remove() (still not done)

Unfortunately I get

java.lang.IndexOutOfBoundsException

I am quite positive there is a very simple way to achieve this but I can't remember it at the moment!

6 Answers 6

1

There is no need to store the indexes of the elements you need to remove. Just remove it directly by:

int size = list.size();
for (int i = size - 1; i >= 1; i--) {
    if (list.get(i).compareTo(list.get(i - 1)) == 0) {
        list.remove(i);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the last iteration of the for loop, list.get(i+1) goes beyond the bounds of the list and thus the IndexOutOfBoundsException. In any array/arraylist, the maximum accessible index is always size/length - 1.

To fix that you need to change your logic a bit.

for (int i = 1; i<list.size(); i++) {
    if (list.get(i-1).compareTo(list.get(i))==0) {
        positionToRemove.add(i);
    }
}

1 Comment

I have always problem with bounds exceptions, but this proved to be very useful for me in understanding how loops works. Many thanks!
0

When you access the List using index + 1 or i+1, you overstep the bounds of the List on the last iteration. You can fix this by setting the conditional for your for loop to i < list.size() -1.

for (int i = 0; i < list.size() -1; i++) {  
        if (list.get(i).compareTo(list.get(i+1))==0) {
            positionToRemove.add(i);
        }
 }

Comments

0

It should not reach the length of your list. You must shutdown the traverses at size -1.

for (int i = 0; i<list.size() - 1; i++) {
    if (list.get(i).compareTo(list.get(i+1))==0) {
        positionToRemove.add(i);
    }
}

Comments

0

Where positionToRemove will at the end contain all the position of the contiguous elements which I will then remove using list.remove() (still not done)

Instead of storing each position, you could start from the end of the list and directly remove the current element if it's the same as its left neighbour. Using this, you don't need to create another list containing the indexes of the objects you want to remove.

List<String> list = new ArrayList<>(Arrays.asList("a","b","c","c","d","a","b","c","d"));
for(int i = list.size() - 1; i > 0; i--){
    if(list.get(i).compareTo(list.get(i-1)) == 0){
        list.remove(i);
    }
}
System.out.println(list);

Which outputs:

[a, b, c, d, a, b, c, d]

Comments

0

You can do this with Java 7, using iterator.

Iterator<Integer> iterator = collection.values().iterator();
Integer previousValue = null;

while(iterator.hasNext()) {
    Integer currentValue = iterator.next();
    if(currentValue.equals(previousValue)){
        iterator.remove();
    }
    previousValue = currentValue;
}

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.