How about this: using StatsBase using Iterators
function uniquepermutations(base) zpos = Vector{Vector{Vector{Int}}}() zval = Vector{Int}() left = length(base) for (v,c) in countmap(base) push!(zpos,collect(subsets(collect(1:left),c))) push!(zval,v) left -= c end res = Vector{Vector{Int}}() for a in product(zpos...) slots = collect(1:length(base)) perm = zeros(length(base)) for (val,b) in zip(zval,a) perm[slots[b]] = val slots[b] = 0 slots = filter(x->x>0,slots) end push!(res,perm) end res end Familiarity with `countmap`,`subsets`,`product`,`zip` from StatsBase and Iterators helps. The logic is straight-forward.