Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close
    Cheat sheets: all cheat sheets

    .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>.Add is O(1) amortized: usually O(1), but O(n) when the backing array is full and has to grow.
    • Dictionary and HashSet lookups are O(1) on average; with many hash collisions they fall toward O(n).
    • SortedDictionary and SortedSet cost O(log n) per key operation. SortedList finds 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: Peek O(1), Enqueue and Dequeue O(log n). Its UnorderedItems lists the heap's internal order, not priority order.
    • Array.Sort and List<T>.Sort are not stable (introsort); LINQ OrderBy is stable.

    Operation costs

    n is the count. Hash costs are averages and assume a reasonable hash code and cheap equality.

    CollectionIndex or key lookupAddRemoveSearch by valueOrder
    T[]O(1)fixed sizefixed sizeO(n); Array.BinarySearch O(log n) if sortedindex order
    List<T>O(1)O(1) amortizedlast item O(1); Remove(x), RemoveAt(0) O(n)O(n); BinarySearch O(log n) if sortedinsertion 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 indexerEnqueue O(1) amortizedDequeue O(1)O(n)first in, first out
    Stack<T>no indexerPush O(1) amortizedPop O(1)O(n)last in, first out
    Dictionary<TKey,TValue>O(1) averageO(1) amortizedO(1) averageContainsValue O(n)none guaranteed
    HashSet<T>Contains O(1) averageO(1) amortizedO(1) averagen/anone 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 resizeO(n)ContainsValue O(n)by key
    SortedSet<T>Contains O(log n)O(log n)O(log n)n/asorted; Min, Max, GetViewBetween
    PriorityQueue<TElement,TPriority>Peek O(1)Enqueue O(log n) amortizedDequeue O(log n)n/aonly the minimum is known
    ConcurrentDictionary<TKey,TValue>O(1) averageO(1) averageO(1) averagen/anone guaranteed
    ImmutableList<T>O(log n)O(log n), returns a new listO(log n) by indexO(n)insertion order
    ImmutableArray<T>O(1)O(n), copiesO(n), copiesO(n)insertion order
    ImmutableDictionary<TKey,TValue>O(log n)O(log n)O(log n)ContainsValue O(n)none guaranteed
    FrozenDictionary, FrozenSetfast; tuned to the keys when builtread-onlyread-onlyn/anone guaranteed
    • Inserting into the middle of a List<T> or an array-backed collection shifts the later items: O(n).
    • Count (and an array's Length) is O(1) on the collections in the table, except ConcurrentDictionary.Count, which takes all its locks.
    • FrozenDictionary and FrozenSet (.NET 8 and later) cost more to create than a Dictionary, in exchange for faster reads.

    What "amortized O(1)" means

    C#Runs
    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}");
    Output
    capacity as it grows: 0 -> 4 -> 8 -> 16 -> 32
    pre-sized to 1000, after 1000 adds: 1000
    • An Add that 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

    C#Runs
    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;
    }
    Output
    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 Equals only on items in its bucket with the same hash code.
    • PriorityQueue dequeues the lowest priority value first, whatever order the items went in.

    Sorting and searching

    APICostStable?
    Array.Sort, List<T>.SortO(n log n), introsort, in placeno
    LINQ OrderBy, ThenBy, and Order (.NET 7 and later)O(n log n) comparisons; buffers the whole sourceyes
    BinarySearch on a sorted array or listO(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 needPick
    Index access, append, iterateList<T> (pre-size it when you know the count)
    Membership test or uniquenessHashSet<T>, with a comparer such as StringComparer.OrdinalIgnoreCase when needed
    Key to value lookupDictionary<TKey,TValue>
    A lookup table built once and read on every requestFrozenDictionary or FrozenSet
    Keys kept sorted, with frequent insertsSortedDictionary<TKey,TValue>
    Keys kept sorted, built once or appended in order, less memorySortedList<TKey,TValue>
    A work queue on one threadQueue<T>; across threads, Channel<T> or ConcurrentQueue<T>
    "Next most urgent"PriorityQueue<TElement,TPriority>
    A map shared by threadsConcurrentDictionary<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 changeImmutableArray<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 a HashSet<T> once: O(n + m) on average.
    • Draining a list with RemoveAt(0) in a loop: O(n²). Use a Queue<T>.
    • Calling Insert(0, x) on a List<T> again and again: O(n²). Append and reverse once, or use a LinkedList<T> or a Stack<T>.
    • Changing a key's equality fields after it is in a Dictionary or HashSet: the lookup can no longer find it.
    • Expecting a Dictionary, or a PriorityQueue's UnorderedItems, 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)?

    A Most adds are O(1), and the O(n) regrowths average out
    B Every add copies the array, but the copy is fast
    C The list allocates its full final size up front
    D Adds are O(1) only after TrimExcess is called
    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?

    A Sort the incoming IDs before the loop
    B Build a HashSet of the known IDs once
    C Use LinkedList for the known IDs
    D Call Contains on an array instead
    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?

    A Priority 5, then 3, then 1
    B The order they were enqueued
    C Priority 1, then 3, then 5
    D The order UnorderedItems lists 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.

    Sources

    TechInterviewNotes Editorial Team · Updated 28 Nov 2026 ·