Home > Back-end >  Deleting arrays that are permutations of another one in Julia
Deleting arrays that are permutations of another one in Julia

Time:04-01

This looks like a fairly easy thing to solve but I've banged my head on this problem for a couple hours now without making progress. I'm hoping someone here could help me. I have an array that contains 2 elements arrays inside. Something like:

a=[[1,2],[3,4],[2,1],[4,3]]

I would like to be able to delete the permutations of arrays inside and keep only one of them. With the previous example, I would like to go from a to:

b=[[1,2],[3,4]]

Using double for loops seems to not work (at least with the ways I tried). I hope you can help me!

Using double for loops seems to not work (at least with the ways I tried) as I came upon the same problem.

CodePudding user response:

Just do Set(Set.(a)):

julia> Set(Set.(a))
Set{Set{Int64}} with 2 elements:
  Set([4, 3])
  Set([2, 1])

And of course you can have a Vector of vectors by using collect and perhaps sort each Vector:

julia> sort.(collect.(Set(Set.(a))))
2-element Vector{Vector{Int64}}:
 [3, 4]
 [1, 2]

CodePudding user response:

You can use unique.

julia> a=[[1,2],[3,4],[2,1],[4,3],[3,3,4],[4,3,3]];
julia> unique(sort,a)
3-element Vector{Vector{Int64}}:
 [1, 2]
 [3, 4]
 [3, 3, 4]

If you need to distinguish the number of each element, you should use sort.

  • Related