Great suggestions but some of them were not allowed (as streams), was very restricted task. Leo's algorithm was the thing I was looking for.
I'm trying to compare a specific letter on arraylist elements and need to remove every bigger letter from the arraylist. This has to be done in linear time, so remove() is not an option. How do I do it?
int deleted = 0;
int n = 0;
while (n < A.size()) {
if (A.get(n).compareTo(x) > 0) {
//removing here
removed = removed + 1;
}
n++;
}
return removed;
A is an arraylist with random alphabetical letters and x is also a random letter. I need to remove every element from A which is bigger than the given x letter. Remove() is not an option because I need to do this in linear time instead of n^2.
ArrayListhas a dynamic size, and the amortized cost makes adding n elements linear (see cs.stackexchange.com/questions/63752/…) hence, it is sufficient to simply add elements.