Skip to content

XCTAssertUnorderedEqualSequences: Improve algorithmic complexity when elements are Hashable #176

Open

Description

@mdznr

In the (only) implementation of XCTAssertUnorderedEqualSequences (where elements are Equatable), it converts the sequence into an Array. Then it iterates through the second sequence, calling firstIndex(of:) on the first array (O(n)), which totals to O(n2). If a Set were used (when the elements are Hashable), then getting the index of an element is O(1), which would make the total algorithmic complexity O(n).

Expected behavior

XCTAssertUnorderedEqualSequences is O(n) when Element: Hashable

Actual behavior

XCTAssertUnorderedEqualSequences is O(n2) when Element: Hashable

Activity

  1. mdznr commented on Dec 21, 2021

    @mdznr
    ContributorAuthor

    Introduced with commit ca8af77

  2. shubhransh-gupta commented on Aug 28, 2026

    @shubhransh-gupta

    Addressed in #285 by adding an $O(n)$ frequency-counting overload of expectUnorderedEqualSequences for Hashable elements, while keeping the Equatable implementation as fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions