# How to define my own "unique" function?

**URL:** <https://discourse.julialang.org/t/how-to-define-my-own-unique-function/94464>\
**Category:** General Usage\
**Created:** [February 11, 2023, 12:16pm UTC](https://discourse.julialang.org/t/how-to-define-my-own-unique-function/94464 "2023-02-11T12:16:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 11, 2023, 12:16pm UTC](https://discourse.julialang.org/t/how-to-define-my-own-unique-function/94464/1 "2023-02-11T12:16:14Z")

</div>

Hello!

Suppose I have a list of tuples given as:

```julia
ps
17-element Vector{Tuple{Int64, Int64, Float64}}:
 (6, 1, 0.1943167542320367)
 (6, 7, 0.3711794598818039)
 (6, 3, 0.24015959913323964)
 (3, 1, 0.10660277812749026)
 (3, 7, 0.15676846198395794)
 (3, 6, 0.24015959913323964)
 (1, 7, 0.18199430878135997)
 (1, 6, 0.1943167542320367)
 (7, 6, 0.3711794598818039)
 (1, 3, 0.10660277812749026)
 (7, 3, 0.15676846198395794)
 (4, 8, 0.18129600615558053)
 (8, 4, 0.18129600615558053)
 (8, 9, 0.12927343763884552)
 (9, 8, 0.12927343763884552)
 (9, 5, 0.17768623230197966)
 (5, 9, 0.17768623230197966)

```

Each element consists of (i,j,d), where i and j are indices and d is the distance between them. How would I then go about removing “repeat entries”, which is for example at the bottom:

```julia
 (9, 5, 0.17768623230197966)
 (5, 9, 0.17768623230197966)

```

Where I have i = j, j = i.

So basically my question is, how do I filter this list of tuples to remove cases in which i and j are “flipped”?

What I should end up with is:

```julia
ps
X-element Vector{Tuple{Int64, Int64, Float64}}:
 (6, 1, 0.1943167542320367) #or (1, 6, 0.1943167542320367)
 (6, 7, 0.3711794598818039) #or (7, 6, 0.3711794598818039)
 (6, 3, 0.24015959913323964) #or (3, 6, 0.24015959913323964)
 (3, 1, 0.10660277812749026) #or (1, 3, 0.10660277812749026)
 (3, 7, 0.15676846198395794) #or (7, 3, 0.15676846198395794)
 (1, 7, 0.18199430878135997)
 (4, 8, 0.18129600615558053) #or (8, 4, 0.18129600615558053)
 (8, 9, 0.12927343763884552) #or (9, 8, 0.12927343763884552)
 (9, 5, 0.17768623230197966) #or (5, 9, 0.17768623230197966)

```

Kind regards

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 11, 2023, 12:37pm UTC](https://discourse.julialang.org/t/how-to-define-my-own-unique-function/94464/2 "2023-02-11T12:37:00Z")

</div>

```julia
unique(e->(e[1]*e[2], e[1]+e[2]), a)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [February 11, 2023, 1:03pm UTC](https://discourse.julialang.org/t/how-to-define-my-own-unique-function/94464/3 "2023-02-11T13:03:54Z")

</div>

Same idea:

```julia
unique(e->minmax(e[1],e[2]), a)

```

and assuming distance is the same when ids reversed:

```julia
unique(sort∘collect, a)

```

The second option is a bit slow and brittle (could err if distances are noisy or can be confused with ids), so use first.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [February 11, 2023, 1:05pm UTC](https://discourse.julialang.org/t/how-to-define-my-own-unique-function/94464/4 "2023-02-11T13:05:46Z")

</div>

```julia
unique(e->(abs(e[1]-e[2]), e[1]+e[2]), a)
unique(e->min(e[1],e[2])^2*max(e[1],e[2])^3, a)
unique(e->sort(first(e,2)), a)

```
