Skip to content
Advertisement

Removing Sublist from ArrayList

For simplicity, let’s say I have an ArrayList whose indices contain exactly one single-digit integer. For instance:

JavaScript

I would like to filter out all occurrences of the sublist 6 0 6, such that the new list becomes:

JavaScript

Is there any way of doing this? Using ListIterator doesn’t seem to work for me, because I have to consider three consecutive elements collectively and I’m honestly not sure how to do that.

Here’s a skeleton of the method I have implemented:

JavaScript

Edit: Again, for simplicity, let’s assume there won’t be cases where we have 60606 or similar.

Advertisement

Answer

You can create an efficient and concise O(nm) solution by using Collections.indexOfSubList:

JavaScript

Ideone Demo

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement