Much cleaner accepted JavaScript solution


  • 0
    D
    
    /**
     * @param {number[]} nums
     * @return {number[][]}
     */
    var permuteUnique = function(nums) {
        
        let res = [];
        let map = {};
        let n = nums.length;
        
        util([], []);
        
        return res;
        
        function util(arr, iarr){
            
            if(arr.length >= n){
                if(!map[arr]){
                    map[arr] = true;
                    res.push(arr.slice());
                }
                
                return;
            }
            
            for(let i = 0; i < nums.length; i++){
                
                if(iarr.includes(i)){
                   continue;
                }
                
                arr.push(nums[i]);
                iarr.push(i);
                
                util(arr, iarr);
                
                arr.pop();
                iarr.pop();
            }
            
        }
        
    };
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.