# Version of \`push!(::Set,v)\` which returns whether \`v\` was already present

**URL:** https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239
**Category:** General Usage
**Tags:** question
**Created:** [December 4, 2020, 8:54am UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239 "2020-12-04T08:54:28Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [December 4, 2020, 8:54am UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239/1 "2020-12-04T08:54:28Z")

</div>

Is there a function which allows me to insert an entry into a set and returns whether the entry was already present? I.e. something like

```julia
function insert!(s::Set, v)
    r = v in s
    push!(s,v)
    return r
end

```

---

<div class="post-metadata">

### Author: ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)
#### Post date: [December 4, 2020, 1:32pm UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239/2 "2020-12-04T13:32:07Z")

</div>

The version you suggested looks fine for everyday use. If you expect that most entries will already be present, then it might be slightly faster to make the `push!` conditional, though this could have different results if it is possible for the keys to be `==` but not `===`.

```julia
function insert!(s::Set, v)
    (r = v in s) || push!(s,v)
    return r
end

```

If speed is really, really important, and you expect most calls to return false, there’s a slightly faster possibilty relying on the internal implementation of `Dict`.

---

<div class="post-metadata">

### Author: ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)
#### Post date: [December 4, 2020, 1:53pm UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239/3 "2020-12-04T13:53:52Z")

</div>

Here’s a better version that avoid the redundancy of hashing twice.

```julia
insert!(S::Set,i) = length(S)==length(push!(S,i))

```

---

<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: [December 4, 2020, 2:39pm UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239/4 "2020-12-04T14:39:29Z")

</div>

You can also use tokens in

[https://github.com/andyferris/Dictionaries.jl#tokens](https://github.com/andyferris/Dictionaries.jl#tokens)

to implement a set, avoiding the double-lookup.

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [December 4, 2020, 4:30pm UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239/5 "2020-12-04T16:30:56Z")

</div>

Sorry, my question wasn’t very clear. I was actually wondering whether Base Julia already provides a function like this. C++ for example does: [std::map\<Key,T,Compare,Allocator\>::insert - cppreference.com](https://en.cppreference.com/w/cpp/container/map/insert)

---

<div class="post-metadata">

### Author: ![malacroi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malacroi/32/19745_2.png) [@malacroi](https://discourse.julialang.org/u/malacroi)
#### Post date: [December 4, 2020, 5:01pm UTC](https://discourse.julialang.org/t/version-of-push-set-v-which-returns-whether-v-was-already-present/51239/6 "2020-12-04T17:01:53Z")

</div>

I don’t think `Base` provides an implementation. A more appropriate comparison to `C++`'s is probably with its hinted insert, which specifically doesn’t provide a return value. From your link:

> Notes
> 
> The hinted insert (4-6) does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such as [std::vector::insert](https://en.cppreference.com/w/cpp/container/vector/insert). This makes it possible to create generic inserters such as [std::inserter](https://en.cppreference.com/w/cpp/iterator/inserter). One way to check success of a hinted insert is to compare [size()](https://en.cppreference.com/w/cpp/container/map/size) before and after.
