# Check whether an array contains a object

**URL:** <https://discourse.julialang.org/t/check-whether-an-array-contains-a-object/80328>\
**Category:** General Usage\
**Created:** [May 1, 2022, 3:13pm UTC](https://discourse.julialang.org/t/check-whether-an-array-contains-a-object/80328 "2022-05-01T15:13:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ke\_xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ke_xu/32/8871_2.png) [@ke\_xu](https://discourse.julialang.org/u/ke_xu)\
**Post date:** [May 1, 2022, 3:13pm UTC](https://discourse.julialang.org/t/check-whether-an-array-contains-a-object/80328/1 "2022-05-01T15:13:32Z")

</div>

Suppose I have an array of some objects,

```julia
struct Obj
v::Int64
end
o1 = Obj(1)
o2 = Obj(1)
os = [o1]

```

if use “in” to check if os contains o2

```julia
o2 in os

```

it returns true. Because the “in” operator compares elements by “==”. Is there any built-in function that compares elements with “===” when checking if an array contains an object? Then for

```julia
isBelong(os, o2)

```

it will return false.

---

<div class="post-metadata">

**Author:** ![bkamins](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bkamins/32/208538_2.png) [@bkamins](https://discourse.julialang.org/u/bkamins)\
**Post date:** [May 1, 2022, 3:23pm UTC](https://discourse.julialang.org/t/check-whether-an-array-contains-a-object/80328/2 "2022-05-01T15:23:23Z")

</div>

Note that:

```julia
julia> o1 === o2
true

```

so in your case there would be no difference between `==` and `===`. The `o1` and `o2` objects are indistinguishable in Julia.

However, if you are looking for `in` suppoting `===` comparison use `IdDict`:

```julia
julia> a = [1]
1-element Vector{Int64}:
 1

julia> b = [1]
1-element Vector{Int64}:
 1

julia> d = IdDict([a, b] .=> nothing)
IdDict{Vector{Int64}, Nothing} with 2 entries:
  [1] => nothing
  [1] => nothing

julia> a in keys(d)
true

julia> [1] in keys(d)
false

```

---

<div class="post-metadata">

**Author:** ![ke\_xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ke_xu/32/8871_2.png) [@ke\_xu](https://discourse.julialang.org/u/ke_xu)\
**Post date:** [May 1, 2022, 3:32pm UTC](https://discourse.julialang.org/t/check-whether-an-array-contains-a-object/80328/4 "2022-05-01T15:32:01Z")

</div>

Thanks for your information!  
My bad, I just found if set the struct mutable the two objects will not be equal.

```julia
julia> mutable struct Obj
       v::Int64
       end

julia> o1 = Obj(1)
Obj(1)

julia> o2 = Obj(1)
Obj(1)

julia> o1 == o2
false

julia> x = [o1]
1-element Vector{Obj}:
 Obj(1)

julia> o1 in x
true

julia> o2 in x
false

```

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [May 1, 2022, 3:38pm UTC](https://discourse.julialang.org/t/check-whether-an-array-contains-a-object/80328/5 "2022-05-01T15:38:52Z")

</div>

> [@ke\_xu](#):
>
> Because the “in” operator compares elements by “==”.

`==` falls back to `===` if you haven’t implemented `isequal` for your types IIRC
