Home > Blockchain >  Remove duplicated Vector at dictionary in Julia
Remove duplicated Vector at dictionary in Julia

Time:11-10

I am looking for solution to remove duplicated value in vector which is in dictionary format at Julia.

Here is my dictionary :

x = Dict{AbstractString,Array{Integer,1}}("A" => [1,2,3], "B" => [3,4,5], "C" => [5,6,7])

and below is expected output :

Dict{AbstractString, Vector{Integer}} with 3 entries:
"A" => [1, 2]
"B" => [4]
"C" => [6, 7]

CodePudding user response:

This is a relatively short way to do it (I have not optimized it for full speed to keep the solution short):

julia> using StatsBase

julia> x = Dict{AbstractString,Array{Integer,1}}("A" => [1,2,3], "B" => [3,4,5], "C" => [5,6,7])
Dict{AbstractString, Vector{Integer}} with 3 entries:
  "B" => [3, 4, 5]
  "A" => [1, 2, 3]
  "C" => [5, 6, 7]

julia> dups = [k for (k, v) in countmap(Iterators.flatten(values(x))) if v > 1]
2-element Vector{Int64}:
 5
 3

julia> foreach(v -> setdiff!(v, dups), values(x))

julia> x
Dict{AbstractString, Vector{Integer}} with 3 entries:
  "B" => [4]
  "A" => [1, 2]
  "C" => [6, 7]

If anything is not clear in the code please comment.

This solution updates your dictionary x in-place as I assumed this is what you wanted.

  • Related