集合

2026年3月26日
1 分

集合の型は Set{T}T は型パラメータ。 次のように初期化する。

julia> s = Set([1, 2])
Set{Int64} with 2 elements:
  2
  1

和集合は union 関数。

julia> union(s, [1, 2, 3])
Set{Int64} with 3 elements:
  2
  3
  1

引数のうちどちらかが集合であればいいらしい。

julia> union([2, 3], s)
3-element Vector{Int64}:
 2
 3
 1

要素の追加。

julia> push!(s, 3)
Set{Int64} with 3 elements:
  2
  3
  1

積集合(共通部分)。

julia> intersect(s, [3, 4])
Set{Int64} with 1 element:
  3

部分集合か否か。

julia> issubset([1, 2], s)
true

数学で使う演算子 も使える。

julia> [1, 2] ⊆ s
true

これは、REPLで \subseteq と入力した後、

julia> [1, 2] \subseteq

TAB キーを押すと変換してくれる。

julia> [1, 2]

\subsetは変換してくれるけど、演算子として定義されてないみたい。

julia> [1, 2] ⊂ s
ERROR: UndefVarError: `⊂` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] top-level scope
   @ REPL[79]:1