# Check whether an array contains a object

Suppose I have an array of some objects,

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

if use “in” to check if os contains o2

``````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

``````isBelong(os, o2)
``````

it will return false.

Note that:

``````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> 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
``````
2 Likes

My bad, I just found if set the struct mutable the two objects will not be equal.

``````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
``````

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

1 Like