Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicated Vector at dictionary in Julia

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading