liveBook · Manning

Membership test

As explained in Julia as a Second Language

A membership test determines whether a specified element belongs to a collection.

A membership test determines whether a specified element belongs to a collection. In Julia, use `in` or its Greek equivalent `∈`; use `⊆` or `issubset` for multiple elements. section 13.2.1

Choose the collection with the test’s needs in mind. Sets provide fast membership checks and fast insertion or removal, while arrays preserve order and support indexed access. For small collections, arrays may be faster; above roughly 30–40 elements, sets outperform unsorted arrays for membership tests. section 13.2.1

Testing one or several elements

julia> A = [2, 7, 9, 10, 11, 12, 15, 18, 19] julia> S = Set(A) julia> 18 in S true julia> 18 ∈ A true julia> [11, 15] ⊆ A true julia> issubset([11, 15], S) true section 13.2.1

Table 13.1 Differences and similarities between sets and arrays (view table figure)

Property

Sets

Arrays

Duplicates allowed

No

Yes

Elements ordered

No

Yes

Random access

No

Yes

Quick membership test

Yes

No

Iterable

Yes

Yes

Compare sets and arrays by ordering, duplicates, random access, iteration, and membership-test speed. section 13.2.1

How Julia as a Second Language covers it

How deep it goes
Explains operators, subset checks, search comparisons, and collection trade-offs without descending into implementation internals.
Examples
Demonstrates `in`, `∈`, `⊆`, and `issubset` with arrays and sets containing integers.
What it assumes
Assumes familiarity with Julia collections while defining membership, subset, set, and array behavior through examples.
What you can do after
Apply membership operators and select sets or arrays according to lookup speed, ordering, uniqueness, and access requirements.
How it connects
Connects membership tests with sets, arrays, binary search, subset checks, ordering, and duplicate handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.