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?
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With