# Findfirst alternative for Set

**URL:** <https://discourse.julialang.org/t/findfirst-alternative-for-set/99271>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 23, 2023, 11:06am UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271 "2023-05-23T11:06:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [May 23, 2023, 11:06am UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/1 "2023-05-23T11:06:24Z")

</div>

Hi there,

I am wondering whether there is something similar to `findfirst` which does not return the index, but the matching object itself.

This would be useful to work with Sets

```julia
julia> findfirst(x->x==1,Set([1,2,3]))
ERROR: MethodError: no method matching keys(::Set{Int64})

Closest candidates are:
  keys(::DataStructures.Accumulator)
   @ DataStructures ~/.julia/packages/DataStructures/59MD0/src/accumulator.jl:58
  keys(::Base.Pairs)
   @ Base iterators.jl:318
  keys(::Tuple)
   @ Base tuple.jl:71
  ...

Stacktrace:
 [1] pairs(collection::Set{Int64})
   @ Base ./abstractdict.jl:172
 [2] findfirst(testf::var"#11#12", A::Set{Int64})
   @ Base ./array.jl:2115
 [3] top-level scope
   @ REPL[25]:1

```

---

<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:** [May 23, 2023, 11:47am UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/2 "2023-05-23T11:47:41Z")

</div>

You could just test for membership:

```julia
julia> 1 in Set([1,2,3])
true

```

Or, if you need to match on more complex criteria:

```julia
julia> any(mymatchfun, Set([1,2,3]))
true

```

(Hmm, but that doesn’t give you the object itself.)

Maybe you can give an example use-case which does not trivially reduce to a simple membership test?

---

<div class="post-metadata">

**Author:** ![HenriDeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrideh/32/8316_2.png) [@HenriDeh](https://discourse.julialang.org/u/HenriDeh)\
**Post date:** [May 23, 2023, 11:57am UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/3 "2023-05-23T11:57:33Z")

</div>

```julia
function myfindfirst(f, s::Set)
   for i in s
       f(i) && return i
   end
   return nothing
end

```

is what you’re looking after ?

---

<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:** [May 23, 2023, 11:58am UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/4 "2023-05-23T11:58:18Z")

</div>

I guess the concept of finding the ‘first’ element that matches something in a set, doesn’t make sense, since sets are not ordered. It would be more akin to random sampling.

---

<div class="post-metadata">

**Author:** ![schlichtanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schlichtanders/32/32145_2.png) [@schlichtanders](https://discourse.julialang.org/u/schlichtanders)\
**Post date:** [May 23, 2023, 12:00pm UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/5 "2023-05-23T12:00:03Z")

</div>

thank you all very much.

I was hoping that there already exists something. But indeed. As the set is random, the name `findfirst` does not make much sense. Still the feature is useful and I very much like the simplicity of the `myfindfirst` function

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [May 23, 2023, 12:06pm UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/6 "2023-05-23T12:06:37Z")

</div>

You can also use `first(Iterators.filter(==(1), s))`. Note that this throws an error if no match is found. You can use `first(..., 1)` to get a 1-element vector if it is found and a 0-element vector if not.

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [May 23, 2023, 12:07pm UTC](https://discourse.julialang.org/t/findfirst-alternative-for-set/99271/7 "2023-05-23T12:07:36Z")

</div>

This thread is effectively asking the same question: [Standard way to get first element of iterator satisfying predicate - #12 by simeonschaub](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/12)
