Cheat Sheet: Speed of Common Operations
add to end |
remove |
insert at middle |
remove |
Random Access |
In-order |
Search for |
Notes
| |
Array |
O(n) |
O(n) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
Most efficient use of memory; use in cases where data size is fixed. |
List<T> |
best case O(1); worst case O(n) |
O(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
Implementation is optimized |
Collection<T> |
best case O(1); worst case O(n) |
O(1) |
O(n) |
O(n) |
O(1) |
O(1) |
O(n) |
List is a better choice, unless |
LinkedList<T> |
O(1) |
O(1) |
O(1) |
O(1) |
O(n) |
O(1) |
O(n) |
Many operations are fast, |
Stack<T> |
best case O(1); worst case O(n) |
O(1) |
N/A |
N/A |
N/A |
N/A |
N/A |
Shouldn't be selected for |
Queue<T> |
best case O(1); worst case O(n) |
O(1) |
N/A |
N/A |
N/A |
N/A |
N/A |
Shouldn't be selected for |
Dictionary<K,T> |
best case O(1); worst case O(n) |
O(1) |
best case O(1); worst case O(n) |
O(1) |
O(1)* |
O(1)* |
O(1) |
Although in-order access time |