Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique elements of an array of arrays with "issetequal"

Tags:

julia

For an array like

v=[[1,2],[1,2,3],[2,3,1]]

I am looking for a method to delete all entries that are duplicated in the sense that they are equal when considered as sets. In this example, issetequal([1,2,3],[2,3,1]) = true, so the method should return the array [[1,2],[1,2,3]].

In principle, something like unique(issetequal, v) would solve the problem. But in practice, this option gives the error

ERROR: MethodError: no method matching issetequal(::Array{Int64,1})

Does anybody have a sugestion?

like image 336
Marco Tulio Angulo Avatar asked Feb 03 '20 14:02

Marco Tulio Angulo


1 Answers

From the documentation, we see that this form of unique takes as first argument a unary function:

unique(f, itr)


  Returns an array containing one value from itr for each unique value produced by f applied to elements of itr.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> unique(x -> x^2, [1, -1, 3, -3, 4])
  3-element Array{Int64,1}:
   1
   3
   4


In your example, issetequal is a binary function that directly checks the set-equality of two values. What you want instead is the Set constructor, which constructs a Set out of an Array. You can then let unique test the equality between sets:

julia> unique(Set, [[1,2],[1,2,3],[2,3,1]])
2-element Array{Array{Int64,1},1}:
 [1, 2]   
 [1, 2, 3]
like image 147
François Févotte Avatar answered Sep 21 '22 14:09

François Févotte