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?
🌐
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()).
Discussions

Why is it that removing an element from the start of an array O(n) while at the end O(1)?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
8
2
September 16, 2023
arrays - Time Complexity for Java ArrayList - Stack Overflow
I found other entries for this question that dealt with specific methods, but nothing comprehensive. I'd like to verify my own understanding of the most often used methods of this data structure: ... More on stackoverflow.com
🌐 stackoverflow.com
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)
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. More on chegg.com
🌐 chegg.com
1
August 5, 2020
Remove in Java in O(n) time instead of O(N^2)

I mentally skipped over the first 'in' in this headline the first time i read it.

More on reddit.com
🌐 r/programming
36
18
October 16, 2016
🌐
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....
🌐
Reddit
reddit.com › r/learnprogramming › why is it that removing an element from the start of an array o(n) while at the end o(1)?
r/learnprogramming on Reddit: Why is it that removing an element from the start of an array O(n) while at the end O(1)?
September 16, 2023 -

From my understanding the reason why removing the last element is O(1) is because you don't need to shift the array in memory. You simply remove the last element and leave the old space empty. So why is it that if you remove the first element that the Array HAS to to shift in memory (making it O(n))?

I don't understand the reasoning, if we are okay with leaving empty space in memory at the end of an array and not shifting all the other things surrounding the array in memory. Then why do we have to shift the array in memory if there is space that the start?

I am not understanding, if it's because the memory is trying to stay compact and no empty spaces are allowed. Then why don't all the other stuff in memory be shifted to the left after new space was cleared once we removed the last element from the array?

🌐
Scaler
scaler.com › home › topics › remove() in java
remove() in Java - Scaler Topics
April 7, 2024 - Object remove(int index); If the ... occurrence. The overall time complexity of both variations of the remove method in ArrayList will be O(N), where N is the number of inputs....
🌐
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 ...
🌐
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".
Find elsewhere
🌐
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.
🌐
YourBasic
yourbasic.org › algorithms › time-complexity-arrays
Time complexity of array/list operations [Java, Python] · YourBasic
CODE EXAMPLE To write fast code, avoid linear-time operations in Java ArrayLists and Python lists. Maps or dictionaries can be efficient alternatives.
🌐
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 ...
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 ...
🌐
LabEx
labex.io › tutorials › python-what-is-the-time-complexity-of-list-append-and-remove-operations-in-python-397728
What is the time complexity of list append and remove operations in Python | LabEx
This means that the time it takes to remove an element from the list grows linearly with the size of the list. The reason for this time complexity is that the list.remove() operation needs to search for the first occurrence of the specified element in the list, and then remove it.
🌐
CodeGym
codegym.cc › java blog › java collections › arraylist removeall() method in java
ArrayList removeAll() method in Java
January 7, 2025 - import java.util.ArrayList; class Person { String name; Person(String name) { this.name = name; } @Override public String toString() { return name; } } public class RemoveCustomObjectExample { public static void main(String[] args) { ArrayList<Person> people = new ArrayList<>(); people.add(new Person("Alice")); people.add(new Person("Bob")); people.add(new Person("Charlie")); System.out.println("Original ArrayList: " + people); // Remove the element at index 0 people.remove(0); System.out.println("After removing element at index 0: " + people); } } ... Understanding the performance implications of removing elements by index is essential for writing efficient code. The remove(int index) method has the following time complexity:
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-remove-an-element-from-arraylist-using-listiterator
Java Program to Remove an Element from ArrayList using ListIterator - GeeksforGeeks
July 23, 2025 - Output : ArrayList = [“Red”, “White”, “Blue”, “Pink”] Remove element "Black" or 5th element in the ArrayList.
🌐
Javabrahman
javabrahman.com › java-8 › java-8-collection-removeif-method-tutorial-with-examples
Java 8 - Collection.removeIf method tutorial with examples
Knowing the specificity of purpose of the removeIf() method, and the need to remove the overhead of shifting of ArrayList elements after a removal from the midde, Java designers overrode the default implementation of removeIf() in the ArrayList (possible as ArrayList implements Collection), and optimize the code while doing so to achieve a time complexity of O(n).
🌐
Medium
yogeshkkhichi.medium.com › time-and-space-complexity-of-collections-5a00c7b1d32b
Time and Space Complexity of Collections | by Yogesh Kumar | Medium
November 17, 2020 - Remove by index: To remove by index, ArrayList find that index using random access in O(1) complexity, but after removing the element, shifting the rest of the elements causes overall O(N) time complexity.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › ArrayList.html
ArrayList (Java Platform SE 8 )
April 21, 2026 - The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. Each ArrayList instance has a capacity.
🌐
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).
🌐
Carnegie Mellon University
cs.cmu.edu › ~mrmiller › 15-121 › Slides › 09-BigO-ArrayList.pdf pdf
Big O & ArrayList 15-121 Fall 2020 Margaret Reid-Miller
September 24, 2020 - Time(n) = 3n = O(3n), not O(2n)! ... A followed by B. • Then the overall complexity of the algorithm is ... Removes all the elements from this list.
🌐
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.