You can use searchsorted to get a range of indices where the value occurs instead of the first, and then use splice! to replace values ββin this range with a new set of values:
insert_and_dedup!(v::Vector, x) = (splice!(v, searchsorted(v,x), [x]); v)
This is a nice little liner that does what you want.
julia> v = [1, 2, 3, 3, 5]; julia> insert_and_dedup!(v, 4) 6-element Array{Int64,1}: 1 2 3 3 4 5 julia> insert_and_dedup!(v, 3) 5-element Array{Int64,1}: 1 2 3 4 5
It made me think splice! should handle the case where the replacement is a single value, not an array, so I can add this function.
source share