The cost of a remove is O(n) as you have to shuffle the elements to the "right" of that point "left" by one:

                 Delete D
                     |
                     V
+-----+-----+-----+-----+-----+-----+-----+
|  A  |  B  |  C  |  D  |  E  |  F  |  G  |
+-----+-----+-----+-----+-----+-----+-----+
                     <------------------
                      Move E, F, G left

If your test code is giving you O(1) then I suspect you're not measuring it properly :-)

The OpenJDK source, for example, has this:

public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index, numMoved);
    elementData[--size] = null; // Let gc do its work

    return oldValue;
}

and the System.arraycopy is the O(n) cost for this function.


In addition, I'm not sure you've thought this through very well:

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

This is going to remove the following elements from the original list:

    0, 2, 4, 8

and so on, because the act of removing element 0 shifts all other elements left - the item that was originally at offset 1 will be at offset 0 when you've deleted the original offset 0, and you then move on to delete offset 1.

Answer from paxdiablo on Stack Overflow
Top answer
1 of 3
30

The cost of a remove is O(n) as you have to shuffle the elements to the "right" of that point "left" by one:

                 Delete D
                     |
                     V
+-----+-----+-----+-----+-----+-----+-----+
|  A  |  B  |  C  |  D  |  E  |  F  |  G  |
+-----+-----+-----+-----+-----+-----+-----+
                     <------------------
                      Move E, F, G left

If your test code is giving you O(1) then I suspect you're not measuring it properly :-)

The OpenJDK source, for example, has this:

public E remove(int index) {
    rangeCheck(index);

    modCount++;
    E oldValue = elementData(index);

    int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index, numMoved);
    elementData[--size] = null; // Let gc do its work

    return oldValue;
}

and the System.arraycopy is the O(n) cost for this function.


In addition, I'm not sure you've thought this through very well:

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

This is going to remove the following elements from the original list:

    0, 2, 4, 8

and so on, because the act of removing element 0 shifts all other elements left - the item that was originally at offset 1 will be at offset 0 when you've deleted the original offset 0, and you then move on to delete offset 1.

2 of 3
16

First off, you are not measuring complexity in this code. What you are doing is measuring (or attempting to measure) performance. When you graph the numbers (assuming that they are correctly measured) you get a performance curve for a particular use-case over a finite range of values for your scaling variable.

That is not the same as a computational complexity measure; i.e. big O, or related Bachman-Landau notations. These are about mathematical limits as the scaling variable tends to infinity.

And this is not just a nitpick. It is quite easy to construct examples1 where performance characteristics change markedly as N gets very large.

What are doing when you graph performance over a range of values and fit a curve is to estimate the complexity.

1 - And a real example is the average complexity of various HashMap functions which switch from O(1) to O(N) (with a very small C) when N reaches 2^31. The modality is because the hash array cannot grow beyond 2^31 slots.


The second point is that that the complexity of ArrayList.remove(index) is sensitive to the value of index as well as the list length.

  • The "advertised" complexity of O(N) for the average and worst cases.

  • In the best case, the complexity is actually O(1). Really!

    This happens when you remove the last element of the list; i.e. index == list.size() - 1. That can be performed with zero copying; look at the code that @paxdiablo included in his Answer.


Now to your Question. There are a number of reasons why your code could give incorrect measurements. For example:

  • You are not taking account of JIT compilation overheads and other JVM warmup effects.

  • I can see places where the JIT compiler could potentially optimize away entire loops.

  • The way you are measuring the time is strange. Try treating this as algebra.

            ((midPoint - start) - (stop - midPoint)) / count;
    

    Now simplify ... and the midPoint term cancels out.

  • You are only removing half of the elements from the list, so you only measuring over the range 50,000 to 100,000 of your scaling variable. (And I expect you are then plotting against the scaling variable; i.e. you are plotting f(N + 5000) against N.

  • The time intervals you are measuring could be too small for the clock resolution on your machine. (Read the javadocs for nanoTime() to see what resolution it guarantees.)

I recommend that people wanting to avoid mistakes like the above should read:

  • How do I write a correct micro-benchmark in Java?
🌐
Scaler
scaler.com › home › topics › remove() in java
remove() in Java - Scaler Topics
April 7, 2024 - However, if the index is not in the range of the ArrayList, the remove method throws IndexOutOfBoundsException. The time complexity of the remove(Object obj) method is O(N).
🌐
Cisc3130fa24
cisc3130fa24.github.io › handouts › ArrayList-complexity.html
time complexity of ArrayList operations
Suppose that list refers to an ArrayList<E>, element is a variable of type E, and index is a random int in the range [0, list.size()). Suppose that iter is an Iterator<E> obtained via list.iterator(). It is possible that iter has been moved forward using iter.next(); that is, the cursor is ...
🌐
Scaler
scaler.com › home › topics › removeall() in java
removeAll() Method in Java - Scaler Topics
May 5, 2024 - Thus the overall time complexity to remove all elements present in the ArrayList from the set is O(n*m).
🌐
CodeGym
codegym.cc › java blog › java collections › arraylist removeall() method in java
ArrayList removeAll() method in Java
January 7, 2025 - Best case: Removing the last element has a time complexity of O(1) as no shifting of elements is required.
🌐
CodingTechRoom
codingtechroom.com › question › time-complexity-java-arraylist-remove-element
What is the Time Complexity of Removing an Element from a Java ArrayList? - CodingTechRoom
ArrayList<String> list = new ArrayList<>(); list.add("Element1"); list.add("Element2"); list.remove("Element1"); // removing an element from ArrayList · The time complexity of the `remove(element)` method in a Java ArrayList is O(n) in the worst case.
🌐
Brainly
brainly.com › computers and technology › high school › what is the time complexity of removing an element from an arraylist in the worst case? a) o(1) b) o(log n) c) o(n) d) o(n^2)
[FREE] What is the time complexity of removing an element from an ArrayList in the worst case? A) O(1) B) O(log - brainly.com
September 22, 2023 - The time complexity of removing an element from an ArrayList in the worst case is O(n). ArrayList is an implementation of a resizable array, and when an element is removed from the middle of the list, all subsequent elements need to be shifted ...
🌐
GitHub
gist.github.com › psayre23 › c30a821239f4818b0709
Runtime Complexity of Java Collections · GitHub
So that way, most of the times you add a new element you just add at the end with O(1) and doing an average, the runtime is constant https://stackoverflow.com/a/45243529/15001063 ... LinkedList remove is only O(1) if you use its iterator.
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › question › -java-arraylist-iterator-remove-time-complexity
What is the Time Complexity of the remove Method in Java's ArrayList Iterator? - CodingTechRoom
When an element is removed, all subsequent elements in the ArrayList must be shifted to fill the gap, leading to a time complexity of O(n) for the shift operation if many elements remain.
🌐
Coderanch
coderanch.com › t › 536419 › java › time-complexity
A time complexity question (Beginning Java forum at Coderanch)
May 2, 2011 - To obtain the logarithmic time you would need a binary search to find the element to remove. Actually, no. Removing an element from an ordered array or ArrayList takes linear time, because after you remove the element, you then have to move an average of N/2 elements down to fill the "hole".
🌐
Baeldung
baeldung.com › home › java › java collections › time complexity of java collections
Time Complexity of Java Collections | Baeldung
September 24, 2025 - So let’s focus first on the time ... time · get() – is always a constant time O(1) operation · remove() – runs in linear O(n) time....
🌐
CodingTechRoom
codingtechroom.com › question › removing-objects-java-arraylist-time-complexity
How to Manage Time Complexity When Removing Objects from a Java ArrayList? - CodingTechRoom
When removing an element by index, the time complexity is O(n) due to the potential need to shift the remaining elements. Removing by value can also result in O(n) complexity as the ArrayList needs to search for the element first.
🌐
Chegg
chegg.com › engineering › computer science › computer science questions and answers › what is the time complexity of arraylist remove(index) method? a. o(2n) o b. o(n^2) o c. on) o d. o(logn)
Solved What is the time complexity of ArrayList | Chegg.com
August 5, 2020 - Answer Option C Reason: In ArrayList we get a dynamic array in which we can insert values or traverse or remove values. Option A is wrong …View the full answer ... What is the time complexity of ArrayList remove(index) method? a. O(2n) O b. O(n^2) O c. on) O d.
🌐
Codekru
codekru.com › home › arraylist clear() method in java
ArrayList clear() method in Java - Codekru
August 12, 2022 - public class Codekru { public static ... System.out.println("ArrayList content after: " + al.toString()); } } ... The average time complexity of the clear() method is O(n), where n is the size of the ArrayList....
🌐
LeetCode
leetcode.com › problems › insert-delete-getrandom-o1 › discuss › 357236 › beat-95-with-explanation-of-this-ArrayList-operation-time-complexity › 324681
beat 95% with explanation of this ArrayList operation time ...
August 12, 2019 - Can you solve this real interview question? Insert Delete GetRandom O(1) - Implement the RandomizedSet class: * RandomizedSet() Initializes the RandomizedSet object. * bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
🌐
GeeksforGeeks
geeksforgeeks.org › java › removing-element-from-the-specified-index-in-java-arraylist
Removing Element from the Specified Index in Java ArrayList - GeeksforGeeks
March 31, 2023 - Size of list: 5 Flower ArrayList = [red-rose, tulip, sun-flower, marie-gold, orchid] Removing element at index = 2 After removing element Size of list: 4 Flower ArrayList = [red-rose, tulip, marie-gold, orchid] Time Complexity: O(n) Auxiliary ...