# How to use unique function?

**URL:** <https://discourse.julialang.org/t/how-to-use-unique-function/5266>\
**Category:** New to Julia\
**Created:** [August 7, 2017, 8:31pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266 "2017-08-07T20:31:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxtremblay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxtremblay/32/1369_2.png) [@maxtremblay](https://discourse.julialang.org/u/maxtremblay)\
**Post date:** [August 7, 2017, 8:31pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266/1 "2017-08-07T20:31:31Z")

</div>

Hello,

I have a vector of a type named `Gate`. I define `==` for it and I checked that `isequal` works as well.

I want to use the `unique` function, but it doesn’t seem to work.

Here a screenshot that show the problem.

![08](https://global.discourse-cdn.com/julialang/original/3X/4/c/4cd65f1c1bef986bb6491356b1edac04b7dce726.png) :

The length of the vector after using unique should be 2, but it is still 4 and I don’t understand since there are in fact only 2 unique elements.

Thank!

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [August 7, 2017, 9:03pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266/2 "2017-08-07T21:03:50Z")

</div>

You also need to implement `hash(x::Gate)`.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 7, 2017, 10:41pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266/3 "2017-08-07T22:41:49Z")

</div>

I think @anon94023334 is exactly right. In case you want to learn more, the documentation for `isequal` mentions that:

```julia
help?> isequal
search: isequal

  isequal(x, y)

  Similar to ==, except treats all floating-point NaN values as equal to each other, and treats -0.0 as unequal
  to 0.0. The default implementation of isequal calls ==, so if you have a type that doesn't have these
  floating-point subtleties then you probably only need to define ==.

  isequal is the comparison function used by hash tables (Dict). isequal(x,y) must imply that hash(x) == hash(y).

```

---

<div class="post-metadata">

**Author:** ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)\
**Post date:** [August 8, 2017, 6:58pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266/4 "2017-08-08T18:58:26Z")

</div>

Thank you @anon94023334, is this documented somewhere? I wouldn’t find it myself by just looking at `unique`’s documentation.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [August 8, 2017, 7:36pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266/5 "2017-08-08T19:36:47Z")

</div>

It’s referenced in the `isequal` docstring:

> isequal is the comparison function used by hash tables (Dict). isequal(x,y)  
> must imply that hash(x) == hash(y).  
> This typically means that if you define your own == function then you must  
> define a corresponding hash (and vice versa). Collections typically  
> implement isequal by calling isequal recursively on all contents.

Though, interestingly enough, it’s not mentioned in the `==` docstring.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 16, 2017, 2:46pm UTC](https://discourse.julialang.org/t/how-to-use-unique-function/5266/7 "2017-08-16T14:46:27Z")

</div>

See also: [https://github.com/JuliaLang/julia/issues/12198](https://github.com/JuliaLang/julia/issues/12198). It’s a recognized problem.
