# Given S = Set{Vector{Int}}, how do I get a reference to the original object in S?

**URL:** https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241
**Category:** General Usage
**Tags:** dictionary
**Created:** [January 11, 2020, 4:43pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241 "2020-01-11T16:43:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![rrhiemstra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rrhiemstra/32/12263_2.png) [@rrhiemstra](https://discourse.julialang.org/u/rrhiemstra)
#### Post date: [January 11, 2020, 4:43pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/1 "2020-01-11T16:43:09Z")

</div>

Given a set S = Set{Vector{Int}}(s\_1,…,s\_n) and a vector v such that  
v == s\_k but not v===s\_k, how do I return the original object s\_k from S?

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [January 11, 2020, 5:02pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/2 "2020-01-11T17:02:19Z")

</div>

```julia
SA = [S...]
s_k = SA[findfirst(isequal(v), SA)]

```

---

<div class="post-metadata">

### Author: ![rrhiemstra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rrhiemstra/32/12263_2.png) [@rrhiemstra](https://discourse.julialang.org/u/rrhiemstra)
#### Post date: [January 11, 2020, 5:14pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/3 "2020-01-11T17:14:59Z")

</div>

I guess this works, but then you lose the O(1) lookup of sets/dicts.

The solution I use right now is to use a dictionary as follows. Given,  
s = [1,1,2]  
S = Dict(hash(s) =\> s)

with v==s and !(v===s), get s as follows  
s = S[hash(v)]

This is efficient and does what I need, but I feel the same should be possible using a set.

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [January 11, 2020, 5:17pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/4 "2020-01-11T17:17:30Z")

</div>

Because you’re comparing objects by value, you need to check elements one-by-one, so it can’t be O(1).

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [January 11, 2020, 5:20pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/5 "2020-01-11T17:20:27Z")

</div>

That’s not how sets work

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [January 11, 2020, 5:23pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/6 "2020-01-11T17:23:38Z")

</div>

Ah, good catch, I’m wrong (I thought it compared vectors by reference).

---

<div class="post-metadata">

### Author: ![rrhiemstra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rrhiemstra/32/12263_2.png) [@rrhiemstra](https://discourse.julialang.org/u/rrhiemstra)
#### Post date: [January 11, 2020, 5:25pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/7 "2020-01-11T17:25:11Z")

</div>

@stillyslalom, my approach using a Dict is O(1).

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [January 11, 2020, 5:38pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/8 "2020-01-11T17:38:29Z")

</div>

Looking at the source, `Set` uses `Dict`s under the hood: [https://github.com/JuliaLang/julia/blob/7e6ef91c9d0bf89de7b45a46aaacf4f644986b69/base/set.jl#L4](https://github.com/JuliaLang/julia/blob/7e6ef91c9d0bf89de7b45a46aaacf4f644986b69/base/set.jl#L4)

so you can use

```julia
s_k = getkey(S.dict, v, Nothing)

```

---

<div class="post-metadata">

### Author: ![rrhiemstra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rrhiemstra/32/12263_2.png) [@rrhiemstra](https://discourse.julialang.org/u/rrhiemstra)
#### Post date: [January 11, 2020, 5:54pm UTC](https://discourse.julialang.org/t/given-s-set-vector-int-how-do-i-get-a-reference-to-the-original-object-in-s/33241/9 "2020-01-11T17:54:14Z")

</div>

@stillyslalom, great catch! That solves it.
