.NET Collections Big-O Cheat Sheet
Compare common collection-operation costs.
What the common operations cost on each mainstream .NET collection, what "amortized O(1)" means, and how to pick a collection from the access pattern.
At a glance
List<T>.Addis O(1) amortized: usually O(1), but O(n) when the backing array is full and has to grow.DictionaryandHashSetlookups are O(1) on average; with many hash collisions they fall toward O(n).SortedDictionaryandSortedSetcost O(log n) per key operation.SortedListfinds a key in O(log n) but inserts in O(n).LinkedList<T>adds and removes at either end in O(1). In the middle it is O(1) only when you already hold the node; finding the node is O(n).PriorityQueue:PeekO(1),EnqueueandDequeueO(log n). ItsUnorderedItemslists the heap's internal order, not priority order.Array.SortandList<T>.Sortare not stable (introsort); LINQOrderByis stable.
Operation costs
n is the count. Hash costs are averages and assume a reasonable hash code and cheap equality.
| Collection | Index or key lookup | Add | Remove | Search by value | Order |
|---|---|---|---|---|---|
T[] | O(1) | fixed size | fixed size | O(n); Array.BinarySearch O(log n) if sorted | index order |
List<T> | O(1) | O(1) amortized | last item O(1); Remove(x), RemoveAt(0) O(n) | O(n); BinarySearch O(log n) if sorted | insertion order |
LinkedList<T> | O(n) | AddFirst, AddLast O(1) | O(1) given the node; Remove(value) O(n) | O(n) | insertion order |
Queue<T> | no indexer | Enqueue O(1) amortized | Dequeue O(1) | O(n) | first in, first out |
Stack<T> | no indexer | Push O(1) amortized | Pop O(1) | O(n) | last in, first out |
Dictionary<TKey,TValue> | O(1) average | O(1) amortized | O(1) average | ContainsValue O(n) | none guaranteed |
HashSet<T> | Contains O(1) average | O(1) amortized | O(1) average | n/a | none guaranteed |
SortedDictionary<TKey,TValue> | O(log n) | O(log n) | O(log n) | ContainsValue O(n) | by key |
SortedList<TKey,TValue> | O(log n); by index O(1) | O(n); O(log n) when added at the end without a resize | O(n) | ContainsValue O(n) | by key |
SortedSet<T> | Contains O(log n) | O(log n) | O(log n) | n/a | sorted; Min, Max, GetViewBetween |
PriorityQueue<TElement,TPriority> | Peek O(1) | Enqueue O(log n) amortized | Dequeue O(log n) | n/a | only the minimum is known |
ConcurrentDictionary<TKey,TValue> | O(1) average | O(1) average | O(1) average | n/a | none guaranteed |
ImmutableList<T> | O(log n) | O(log n), returns a new list | O(log n) by index | O(n) | insertion order |
ImmutableArray<T> | O(1) | O(n), copies | O(n), copies | O(n) | insertion order |
ImmutableDictionary<TKey,TValue> | O(log n) | O(log n) | O(log n) | ContainsValue O(n) | none guaranteed |
FrozenDictionary, FrozenSet | fast; tuned to the keys when built | read-only | read-only | n/a | none guaranteed |
- Inserting into the middle of a
List<T>or an array-backed collection shifts the later items: O(n). Count(and an array'sLength) is O(1) on the collections in the table, exceptConcurrentDictionary.Count, which takes all its locks.FrozenDictionaryandFrozenSet(.NET 8 and later) cost more to create than aDictionary, in exchange for faster reads.
What "amortized O(1)" means
var list = new List<int>();
var capacities = new List<int> { list.Capacity };
for (var i = 0; i < 20; i++)
{
list.Add(i);
if (list.Capacity != capacities[^1]) capacities.Add(list.Capacity);
}
Console.WriteLine($"capacity as it grows: {string.Join(" -> ", capacities)}");
var sized = new List<int>(1_000);
for (var i = 0; i < 1_000; i++) sized.Add(i);
Console.WriteLine($"pre-sized to 1000, after 1000 adds: {sized.Capacity}");capacity as it grows: 0 -> 4 -> 8 -> 16 -> 32 pre-sized to 1000, after 1000 adds: 1000
- An
Addthat finds the array full allocates a bigger one and copies every item: that one call is O(n). - The capacity doubles, so the copies add up to less than 2n over n adds: O(1) per add on average.
- The 0, 4, 8, 16 sequence is today's implementation, not a documented contract. The documented rule: O(1) unless the capacity must grow, then O(n).
- When you know the count,
new List<T>(n)avoids every regrowth.
Linear or hashed Contains
var counter = new Counter();
var keys = Enumerable.Range(0, 1_000).Select(i => new Key(i, counter)).ToList();
var set = new HashSet<Key>(keys);
counter.Calls = 0; keys.Contains(new Key(999, counter));
Console.WriteLine($"List.Contains(last item): {counter.Calls} Equals calls");
counter.Calls = 0; set.Contains(new Key(999, counter));
Console.WriteLine($"HashSet.Contains: {counter.Calls} Equals call");
var queue = new PriorityQueue<string, int>();
queue.Enqueue("low", 5); queue.Enqueue("urgent", 1); queue.Enqueue("mid", 3);
Console.WriteLine($"dequeue order: {queue.Dequeue()}, {queue.Dequeue()}, {queue.Dequeue()}");
sealed class Counter { public int Calls; }
sealed class Key(int value, Counter counter) : IEquatable<Key>
{
public int Value { get; } = value;
public bool Equals(Key? other) { counter.Calls++; return other is not null && other.Value == Value; }
public override bool Equals(object? obj) => Equals(obj as Key);
public override int GetHashCode() => Value;
}List.Contains(last item): 1000 Equals calls HashSet.Contains: 1 Equals call dequeue order: urgent, mid, low
- The list compares the probe with every item until it finds a match; the set hashes the probe and calls
Equalsonly on items in its bucket with the same hash code. PriorityQueuedequeues the lowest priority value first, whatever order the items went in.
Sorting and searching
| API | Cost | Stable? |
|---|---|---|
Array.Sort, List<T>.Sort | O(n log n), introsort, in place | no |
LINQ OrderBy, ThenBy, and Order (.NET 7 and later) | O(n log n) comparisons; buffers the whole source | yes |
BinarySearch on a sorted array or list | O(log n) | n/a |
A stable sort keeps items with equal keys in their original order. Sort by a second key with ThenBy, not by sorting twice with an unstable sort.
Choosing by access pattern
| You need | Pick |
|---|---|
| Index access, append, iterate | List<T> (pre-size it when you know the count) |
| Membership test or uniqueness | HashSet<T>, with a comparer such as StringComparer.OrdinalIgnoreCase when needed |
| Key to value lookup | Dictionary<TKey,TValue> |
| A lookup table built once and read on every request | FrozenDictionary or FrozenSet |
| Keys kept sorted, with frequent inserts | SortedDictionary<TKey,TValue> |
| Keys kept sorted, built once or appended in order, less memory | SortedList<TKey,TValue> |
| A work queue on one thread | Queue<T>; across threads, Channel<T> or ConcurrentQueue<T> |
| "Next most urgent" | PriorityQueue<TElement,TPriority> |
| A map shared by threads | ConcurrentDictionary<TKey,TValue>: single operations are thread-safe, but GetOrAdd and AddOrUpdate can run their delegates more than once, and a sequence of calls is not atomic |
| A collection callers cannot change | ImmutableArray<T> for read-heavy use, ImmutableList<T> for frequent changes |
Common mistakes
list.Contains(x)inside a loop over another list: O(n·m). Build aHashSet<T>once: O(n + m) on average.- Draining a list with
RemoveAt(0)in a loop: O(n²). Use aQueue<T>. - Calling
Insert(0, x)on aList<T>again and again: O(n²). Append and reverse once, or use aLinkedList<T>or aStack<T>. - Changing a key's equality fields after it is in a
DictionaryorHashSet: the lookup can no longer find it. - Expecting a
Dictionary, or aPriorityQueue'sUnorderedItems, to enumerate in order. - A hash code that returns the same value for many keys: every lookup degrades toward O(n).
In an interview
Why is List<T>.Add "amortized O(1)" and not O(1)? Most adds write into free space in the array: O(1). When the array is full, the list allocates a bigger one and copies every item: O(n). The capacity doubles, so the copying averages out to O(1) per add.
SortedDictionary or SortedList? Both keep keys sorted and find a key in O(log n). SortedDictionary is a tree with O(log n) inserts and removes; SortedList is two sorted arrays with O(n) inserts, but it uses less memory and is fast to fill from data already in order.
What is the worst case of a Dictionary lookup, and why? O(n), when many keys share a hash code (or a bucket) and the lookup has to compare against all of them. A good hash code keeps lookups near O(1).
How do you turn an O(n·m) "find matches" loop into O(n + m)? Put one list's keys into a HashSet<T> (or a Dictionary) once, then do one O(1) average lookup per item of the other list.
Is a PriorityQueue enumerated in priority order? What does Dequeue cost? No. You cannot foreach the queue itself, and its UnorderedItems lists the heap's internal order. Dequeue removes the minimum in O(log n), and Peek reads it in O(1).
ImmutableArray<T> or ImmutableList<T>? ImmutableArray<T> indexes in O(1) but copies the whole array on every "add". ImmutableList<T> is a balanced tree: O(log n) for both indexing and adding.
Check yourself
Why is List<T>.Add described as amortized O(1)?
Show answer
Correct answer: A
An add is O(1) while there is free capacity; when the array is full it grows by doubling and copies every item, which averages to O(1) per add.
Option B: Only the add that hits a full array copies it.
Option C: A new List<T>() starts with capacity 0 and grows as items arrive.
Option D: TrimExcess may shrink spare capacity, which makes later adds regrow sooner; it does not make adds O(1).
You check 10,000 incoming IDs against a list of 50,000 known IDs. What is the usual fix for the slow List.Contains loop?
Show answer
Correct answer: B
Building the set is O(m) once, then each check is O(1) on average: O(n + m) in place of O(n·m).
Option A: Sorting the incoming IDs does not make a linear scan of the known IDs faster.
Option C: A linked list still searches in O(n) per check.
Option D: An array's Contains is a linear scan too.
Three items are enqueued in a PriorityQueue<string, int> with priorities 5, 1 and 3. In what order does Dequeue return them?
Show answer
Correct answer: C
PriorityQueue is a min-heap: Dequeue always removes the item with the lowest priority value.
Option A: That is highest first; the default comparer puts the smallest value first.
Option B: Priority decides the order, not the insertion order.
Option D: UnorderedItems lists the heap's internal order, which is not sorted.
Related on TechInterviewNotes
- Time and Space Complexity in LINQ (course): start with Big-O for .NET Developers, then Contains, Any and Count: Fast Paths on List, HashSet and IEnumerable and Nested Queries O(n·m) vs Join and HashSet O(n+m)
- Choosing Collections and Understanding Their Complexity (Learn C# and .NET 10, lesson 13) and the C# Collections and Complexity Companion
- FrozenDictionary and FrozenSet
- ConcurrentDictionary: Thread Safety Is Not Workflow Atomicity
- Intermediate C# Equality and Hash Collections (course)
Sources
- Collections and data structures: the amortized and worst-case cost table, mutable against immutable.
- List<T>.Add: O(1), or O(n) when the capacity must grow.
- SortedList<TKey,TValue>.Add: O(n), O(log n) at the end of the list.
- PriorityQueue<TElement,TPriority>: a quaternary min-heap, unordered enumeration.
- Array.Sort: introsort, an unstable sort.
- Enumerable.OrderBy: a stable sort.
TechInterviewNotes Editorial Team · Updated 28 Nov 2026 ·