# Get an equal element of a set/get an equal key of a dictionary

**URL:** https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779
**Category:** General Usage
**Tags:** question, dictionary, set, sets, dictionaries
**Created:** [May 7, 2025, 8:54am UTC](https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779 "2025-05-07T08:54:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [May 7, 2025, 8:54am UTC](https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779/1 "2025-05-07T08:54:31Z")

</div>

Suppose I have `x` and `s::AbstractSet` and I know `x ∈ s`. How to get the element of `s`, say `e`, such that `x == e`? I could iterate through all elements like this, but surely there should be a simpler and more efficient solution?

```julia
function get_equal_element(x, s)
    for e ∈ s
        if x == e
            return e
        end
    end
    throw(ArgumentError("x ∉ s"))
end

```

The same question could be asked about the keys of an `AbstractDict` dictionary. Or just about `Set` and `Dict`, at least.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [May 7, 2025, 12:44pm UTC](https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779/2 "2025-05-07T12:44:07Z")

</div>

The solution for `Dict` is `getkey`:

```julia-repl
julia> getkey(Dict(7.0 => "seven"), 7, nothing)
7.0

```

~~The solution for `Set` should be simply `get`, however the relevant methods are missing.~~ Not sure what to do with sets, though.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [May 7, 2025, 3:16pm UTC](https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779/3 "2025-05-07T15:16:15Z")

</div>

I’m confused - if `x` is in `S`, kind of by definition you already have `e`, since knowing `x in S` implies `hash(x) == hash(e)` (and by extension, barring hash collisions, `isequal(x,e)`). So at best the only thing left would be converting `x` to the element type of `S`, but then you’re looking for more than hash equality.

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [May 7, 2025, 3:43pm UTC](https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779/4 "2025-05-07T15:43:24Z")

</div>

I want the exact object that’s in the `Set`, though. That is, the return value of `get_equal_element(x, s)` must be identical (`===`) to the element of `s` that’s equal to `x`. I know that `x == e`, but conversion (`oftype(e, x)`) doesn’t cut it.

The specific use case is basically this: I have a certain data structure which has certain `Vector` objects at some places. Some of these `Vector`s are equal to some of the other ones, so my goal is to deduplicate them to maybe save memory and improve cache locality when using the data structure. This is my deduplication algorithm:

- Step 1: `push!` each `Vector` value from the data structure into a newly-created `Set`, say `s`.
- Step 2: for each `Vector` value, say `x`, in the data structure, replace it with `get_equal_element(x, s)`.

I don’t really care about the performance of the deduplication itself, so I currently just use the implementation of `get_equal_element` as given above. If I cared about performance I would switch to using `Dict{T, Nothing}` directly instead of `Set{T}`, and then use `getkey` instead of `get_equal_element`.

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 7, 2025, 3:52pm UTC](https://discourse.julialang.org/t/get-an-equal-element-of-a-set-get-an-equal-key-of-a-dictionary/128779/5 "2025-05-07T15:52:26Z")

</div>

> [@nsajko](#):
>
> I know that `x == e`

You even know `isequal(x, e)`.
