slow javascript solution with easy concept


  • 0
    B

    3 modification base on subset problem.

    var subsetsWithDup = function(nums) {
        if (nums === null || nums.length === 0) return [];
    
        var res = [[]];
        var map = {};
    
        // sort array to void duplicate
        nums.sort();
    
        for (value of nums) {
          for (i in res) {
            var temp = res[i].slice();
            temp.push(value);
            
            // transform into unique string using regx
            var str = temp.toString().replace(/\,/g, '');
    
            // using object key to check duplicate
            if (!map[str]) {
              map[str] = true;
              res.push(temp);
            }
    
          }
        }
        return res;
    };
    

Log in to reply
 

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