# Removing the elements of one array from another using setdiff

**URL:** https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977
**Category:** General Usage
**Created:** [December 25, 2017, 12:22am UTC](https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977 "2017-12-25T00:22:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Qiyamah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qiyamah/32/2660_2.png) [@Qiyamah](https://discourse.julialang.org/u/Qiyamah)
#### Post date: [December 25, 2017, 12:22am UTC](https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977/1 "2017-12-25T00:22:11Z")

</div>

Hi fellows,

The following code works:

```nohighlight
a = [1,2,3,4,5]
b = [4,5,6]
setdiff(a,b)
3-element Array{Int64,1}:
 1
 2
 3

```

However this one doesn’t:

```nohighlight
type X
           a::Int
 end
Base.broadcast(::typeof(==),x::X,y::X) = x.a == y.a
a = [X(1),X(2),X(4),X(5),X(6)]
b = [X(4),X(5),X(6)]
setdiff(a,b)
5-element Array{X,1}:
 X(1)
 X(2)
 X(4)
 X(5)
 X(6)

```

What went wrong here?

---

<div class="post-metadata">

### Author: ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)
#### Post date: [December 25, 2017, 1:27am UTC](https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977/2 "2017-12-25T01:27:30Z")

</div>

I’m on julia 0.6.2. I think this is what you might want to do in your code instead of the `broadcast` thing.

```julia
import Base.==
==(x::X,y::X) = x.a == y.a

```

This gives you `X(1) == X(1)`, not the `broadcast` function.  
But this still cannot give you the `setdiff` result you want, at least on my julia v0.6.2. As the result is totally random on my julia 0.6.2. Sometime the result is a four element Array, sometime the result is a five element one.

I try to further dig into the issue. `setdiff(a, b)` leads to `in(a_elem, bset)` leads to `haskey(bset.dict, a_elem)`, where `bset = Set(b)`. And the following happens:

```julia
julia> haskey(Set(b).dict, Y(5))
false

julia> haskey(Set(b).dict, Y(5))
false

julia> haskey(Set(b).dict, Y(5))
true

```

So I think this problem is related to the dictionary in julia 0.6.2. I may have seen some issues similar to this before and the solution to it, and I will try to search for that.

---

<div class="post-metadata">

### Author: ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)
#### Post date: [December 25, 2017, 1:29am UTC](https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977/3 "2017-12-25T01:29:38Z")

</div>

I suspect the reason is because `type` is mutable so even though the `a`s are the same, the objects aren’t equal. Try immutable.

```julia
julia> type mtest
       a::Int
       end

julia> immutable itest
       a::Int
       end

julia> m1 = mtest(1); m2 = mtest(1); m1 == m2
false

julia> i1 = itest(1); i2 = itest(1); i1 == i2
true

```

---

<div class="post-metadata">

### Author: ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)
#### Post date: [December 25, 2017, 1:40am UTC](https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977/4 "2017-12-25T01:40:53Z")

</div>

`immutable` instead of `type` solves the problem on julia 0.6.2.  
And the problem of `type` seems to relate to this:  
[https://github.com/JuliaLang/julia/issues/12198](https://github.com/JuliaLang/julia/issues/12198)

---

<div class="post-metadata">

### Author: ![anon67531922](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@anon67531922](https://discourse.julialang.org/u/anon67531922)
#### Post date: [December 25, 2017, 2:05am UTC](https://discourse.julialang.org/t/removing-the-elements-of-one-array-from-another-using-setdiff/7977/5 "2017-12-25T02:05:56Z")

</div>

Yep. That seems to be the case.

```julia
julia> m1 = [mtest(i) for i in 1:5]; m2 = [mtest(i) for i in 4:5]; setdiff(m1,m2)
5-element Array{mtest,1}:
 mtest(1)
 mtest(2)
 mtest(3)
 mtest(4)
 mtest(5)

julia> i1 = [itest(i) for i in 1:5]; m2 = [itest(i) for i in 4:5]; setdiff(i1,i2)
3-element Array{itest,1}:
 itest(1)
 itest(2)
 itest(3)

```
