# Checking the uniqueness of a tuple

**URL:** <https://discourse.julialang.org/t/checking-the-uniqueness-of-a-tuple/27161>\
**Category:** General Usage\
**Created:** [August 5, 2019, 3:12am UTC](https://discourse.julialang.org/t/checking-the-uniqueness-of-a-tuple/27161 "2019-08-05T03:12:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)\
**Post date:** [August 5, 2019, 3:12am UTC](https://discourse.julialang.org/t/checking-the-uniqueness-of-a-tuple/27161/1 "2019-08-05T03:12:32Z")

</div>

I have an array of tuples, i.e. `Array{Tuple{Int64, Int64}}` which I would like to filter by unique items. That is, my uniqueness criteria for a tuple is that (x\_1, x\_2) = (x\_2, x\_1).

So if I have `a = [(1, 2), (3, 4), (2, 1)] ` I basically want a return value of `ret = [(1, 2), (3,4)]`(or equivalently `[(3, 4), (2, 1)]`.

My current solution is simple, but costly in terms for performance. Solution is

```julia
 unique(x -> Set(x), tmp) ## get the unique pairs.

```

where `tmp` is the array of tuples.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 5, 2019, 3:30am UTC](https://discourse.julialang.org/t/checking-the-uniqueness-of-a-tuple/27161/2 "2019-08-05T03:30:16Z")

</div>

JuMP defines an `UnorderedPair`; could copy that code, turn your tuples into `UnorderedPairs`, and use `unique`/`unique!`/`isunique` as usual. I think this should be in Base actually.

[https://github.com/JuliaOpt/JuMP.jl/blob/ebf9ff93a03f05327e2d082b1fa1175050c8648c/src/quad\_expr.jl#L19-L27](https://github.com/JuliaOpt/JuMP.jl/blob/ebf9ff93a03f05327e2d082b1fa1175050c8648c/src/quad_expr.jl#L19-L27)

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [August 5, 2019, 4:20am UTC](https://discourse.julialang.org/t/checking-the-uniqueness-of-a-tuple/27161/3 "2019-08-05T04:20:24Z")

</div>

If you know that you only need it for pairs, maybe just sort the tuples

```julia
unique(((x, y),) -> x > y ? (y, x) : (x, y), [(1, 2), (3, 4), (2, 1)])

```

?

In the future it’ll be just `unique(sort, ...)` once this is merged:  
[https://github.com/JuliaLang/julia/pull/32710](https://github.com/JuliaLang/julia/pull/32710)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 5, 2019, 9:55am UTC](https://discourse.julialang.org/t/checking-the-uniqueness-of-a-tuple/27161/4 "2019-08-05T09:55:00Z")

</div>

Do you need the original order for anything? If not, it may make sense to just sanitize the input. Eg

```julia
struct OrderedPair{T}
    a::T
    b::T
    function OrderedPair(a::T, b::T) where T
        if !isless(a, b)
            b, a = a, b
        end
        new{T}(a, b)
    end
end

OrderedPair(ab::Tuple{T,T}) where {T} = OrderedPair(ab...)

```

and then

```julia
input = [(1, 2), (3, 4), (2, 1)]
ordered = OrderedPair.(input)
unique(ordered)

```
