# How to find the size of a Set?

**URL:** <https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438>\
**Category:** New to Julia\
**Created:** [June 19, 2019, 9:59am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438 "2019-06-19T09:59:21Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [June 19, 2019, 9:59am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/1 "2019-06-19T09:59:21Z")

</div>

A very basic question:

```julia
julia> S = Set([1,2,3,4])
Set([4, 2, 3, 1])

julia> size(S)
ERROR: MethodError: no method matching size(::Set{Int64})
Closest candidates are:
  size(::BitArray{1}) at bitarray.jl:77
  size(::BitArray{1}, ::Any) at bitarray.jl:81
  size(::Core.Compiler.StmtRange) at show.jl:1561
  ...
Stacktrace:
 [1] top-level scope at none:0

```

How can I find the number of elements in a Set?

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [June 19, 2019, 10:01am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/2 "2019-06-19T10:01:12Z")

</div>

Answering my own question: for some reason, the number of elements in a Set is called “length”:

```julia
julia> length(S)
4

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 19, 2019, 10:10am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/3 "2019-06-19T10:10:21Z")

</div>

> [@Nathaniel](#):
>
> for some reason

The reason is that `Set` is a collection.

[https://docs.julialang.org/en/v1/base/collections/#Set-Like-Collections-1](https://docs.julialang.org/en/v1/base/collections/#Set-Like-Collections-1)

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [June 19, 2019, 10:14am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/4 "2019-06-19T10:14:05Z")

</div>

I am unsure of the sense in which that constitutes a reason. But it doesn’t matter. The name of the method is slightly non-obvious, but presumably this page will now come up if someone else googles it.

---

<div class="post-metadata">

**Author:** ![pkofod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pkofod/32/2179_2.png) [@pkofod](https://discourse.julialang.org/u/pkofod)\
**Post date:** [June 19, 2019, 10:17am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/5 "2019-06-19T10:17:47Z")

</div>

As the documentation for `size` tells you,

```julia
 Return a tuple containing the dimensions of A. 

```

the set has no dimensions (shape), so `size` is not the right abstraction. `length` will always give you the number of elements, and for that reason it applies to `Set`.

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [June 19, 2019, 10:20am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/6 "2019-06-19T10:20:52Z")

</div>

Yes, but logically the term “length” doesn’t make sense for a set, because, well, sets aren’t the sort of things that have length. So the name “length” is not so easy to guess if you don’t already know it. But I did guess it all the same, and posted my answer, so that’s the end of the problem really. Nothing more to see here.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 19, 2019, 10:30am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/7 "2019-06-19T10:30:32Z")

</div>

> [@Nathaniel](#):
>
> “length” doesn’t make sense for a set, because, well, sets aren’t the sort of things that have length

As @pkofod pointed out, `length` is for the number of element in a collection. `Set` is a collection, so it has `length`.

Whether it is intuitive or not is of course subjective, but perhaps as you learn Julia you will find it reasonable. One advantage is that once you learn about `length` ([which comes way before in the docs](https://docs.julialang.org/en/v1/manual/arrays/#Basic-Functions-1)), you can apply it to _all_ sorts of collections — it is in this sense that `length` is said to be _generic_.

Generic building blocks are key to using Julia efficiently: you can write code that should work for a wider range of collections, regardless of whether they are `Set`s. Eg

```julia
julia> function dont_like_4!(x)
           4 ∈ x && empty!(x)
           x
       end
dont_like_4! (generic function with 1 method)

julia> dont_like_4!([1,2,3])
3-element Array{Int64,1}:
 1
 2
 3

julia> dont_like_4!(Set([1,2,3]))
Set([2, 3, 1])

julia> dont_like_4!([1,2,3,4])
0-element Array{Int64,1}

julia> dont_like_4!(Set([1,2,3,4]))
Set(Int64[])

```

Finally, I am curious if you have an alternative suggestion that would be more easily discoverable.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [June 19, 2019, 10:55am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/8 "2019-06-19T10:55:29Z")

</div>

FWIW, you could make a PR that overloads `size(s::Set)` and returns a better error message that points users to `length`. Don’t know if this will be accepted though.

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [June 19, 2019, 11:16am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/9 "2019-06-19T11:16:28Z")

</div>

No, no, look, you’re all dragging this out way longer than it needs to be. I understand perfectly well that there’s an internal consistency to the naming of methods in any language, and that in this case, the logical choice was ‘length’ and not ‘size’. The only problem was that ‘length’ was hard to guess, and the documentation was hard to find. (Try googling “julia set”.)

**These problems are solved by the existence of the first two posts in this thread** , which will hopefully appear when someone else googles the same question. There is really no need for any further discussion at all.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [June 19, 2019, 11:32am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/10 "2019-06-19T11:32:41Z")

</div>

Matlab does some strange things with the `length` function; for example, for an array, `A`, it is equal to `max(size(A))`.

But they have a `numel` function which is basically the same as Julia’s length. I’m not sure how discoverable it would be, except for former Matlab users, but it actually makes more sense to me than `length`.

I suppose it would go well along with `eltype`…

---

<div class="post-metadata">

**Author:** ![czylabsonasa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/czylabsonasa/32/8663_2.png) [@czylabsonasa](https://discourse.julialang.org/u/czylabsonasa)\
**Post date:** [November 1, 2021, 10:34am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/11 "2021-11-01T10:34:04Z")

</div>

There are offline docs available for your julia install, which can be accessed by pointing your browser to:  
`file:///abs_path_to_your_julia_install_dir/share/doc/julia/html/en/index.html`  
(at least on linux :-))

---

<div class="post-metadata">

**Author:** ![Nathaniel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathaniel/32/37854_2.png) [@Nathaniel](https://discourse.julialang.org/u/Nathaniel)\
**Post date:** [November 2, 2021, 5:28am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/12 "2021-11-02T05:28:40Z")

</div>

How can I stop receiving emails about this topic? The problem was solved, by me, in my second post. I don’t want to hear anything else about it.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [November 2, 2021, 5:52am UTC](https://discourse.julialang.org/t/how-to-find-the-size-of-a-set/25438/13 "2021-11-02T05:52:42Z")

</div>

Not sure about emails, but at the bottom of the Discourse web interface there’s a drop down menu, that probably has selected “Tracking”. You can change that to “Muted”.

Perhaps that will stop the emails too. I’m not sure, though, I never keep track of these update emails, they just accumulate in an email folder I never open.

You could also request a moderator to close the thread.

But is this amount of traffic really such an onus?
