1var permuteUnique = function (nums) {2const answer = [];34function perm(pos, array) {5if (pos >= array.length) {6answer.push(array);7}89const setObject = new Set();1011for (let index = pos; index < array.length; index++) {12if (setObject.has(array[index])) {13continue;14}15setObject.add(array[index]);1617// swap numbers18let temp = array[pos];19array[pos] = array[index];20array[index] = temp;2122perm(pos + 1, [...array]);2324// undo swapping for next iteration25temp = array[index];26array[index] = array[pos];27array[pos] = temp;28}29}3031perm(0, nums);3233return answer;34};