var permuteUnique = function (nums) { nums.sort((a, b) => a - b) let prevVal = null let curItems = [[]] for (const val of nums) { const nextItems = [] for (const items of curItems) { let end = items.length if (val === prevVal) { end = items.indexOf(val) } for (let j = 0; j <= end; ++j) { const curVals = [...items] curVals.splice(j, 0, val) nextItems.push(curVals) } } prevVal = val curItems = nextItems } return curItems }