<commit_before>/**
 * @since 2017-03-28 11:55:56
 * @author vivaxy
 * @see https://leetcode.com/problems/subsets-ii/

 Given a collection of integers that might contain duplicates, nums, return all possible subsets.

 Note: The solution set must not contain duplicate subsets.

 For example,
 If nums = [1,2,2], a solution is:

 ```
 [
 [2],
 [1],
 [1,2,2],
 [2,2],
 [1,2],
 []
 ]
 ```
 */

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsetsWithDup = function(nums) {
    var results = [];
    var isSameArray = function(a, b) {
        return JSON.stringify(a) === JSON.stringify(b);
    };
    var saveInResults = function(array) {
        for (var k = 0; k < results.length; k++) {
            if (isSameArray(results[k], array)) {
                return;
            }
        }
        results.push(array);
    };

    var pickOne = function(from, callback) {
        for (var i = from; i < nums.length; i++) {
            callback(nums[i], i);
        }
    };

    var pickTwo = function(from, callback) {
        pickOne(from, function(v1, index) {
            pickOne(index, function(v2) {
                callback([v1, v2]);
            });
        });
    };

    for (var l = 0; l < nums.length; l++) {
        // through length
        for (var i = 0; i < nums.length; i++) {
            // through from
        }
    }
    return results;
};

console.log(subsetsWithDup([1, 2, 2]));
console.log(JSON.stringify(subsetsWithDup([1, 2, 2])) === JSON.stringify([[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]));
console.log(JSON.stringify(subsetsWithDup([1, 2, 3])) === JSON.stringify([[], [1], [1, 2], [1, 2, 2], [2], [2, 2]]));<commit_msg>feat(subsets-ii.js): :sparkles:<commit_after>/**
 * @since 2017-03-28 11:55:56
 * @author vivaxy
 * @see https://leetcode.com/problems/subsets-ii/

 Given a collection of integers that might contain duplicates, nums, return all possible subsets.

 Note: The solution set must not contain duplicate subsets.

 For example,
 If nums = [1,2,2], a solution is:

 ```
 [
 [2],
 [1],
 [1,2,2],
 [2,2],
 [1,2],
 []
 ]
 ```
 */

/**
 * @see https://leetcode.com/submissions/detail/98492145/
 * @param {number[]} nums
 * @return {number[][]}
 */
var subsetsWithDup = function(nums) {
    nums = nums.sort(function(a, b) {
        return a > b;
    });
    var results = [[]];
    var isSameArray = function(a, b) {
        return JSON.stringify(a) === JSON.stringify(b);
    };
    var saveInResults = function(array) {
        for (var k = 0; k < results.length; k++) {
            if (isSameArray(results[k], array)) {
                return;
            }
        }
        results.push(array);
    };

    var pick = function(subArray, n, array) {
        // get n from subArray
        // get one from subArray, then get n-1 from next subArray
        for (var i = 0; i < subArray.length - (n - 1); i++) {
            var current = subArray[i];
            var nextSubArray = subArray.slice(i + 1);
            var resultArray = array.slice();
            resultArray.push(current);
            if (n - 1 > 0) {
                pick(nextSubArray, n - 1, resultArray);
            } else {
                // do things
                saveInResults(resultArray);
            }
        }
    };

    for (var i = 0; i < nums.length; i++) {
        pick(nums, i + 1, []);
    }

    return results;
};

console.log(JSON.stringify(subsetsWithDup([1, 2, 2])) === JSON.stringify([[], [1], [2], [1, 2], [2, 2], [1, 2, 2]]));
console.log(JSON.stringify(subsetsWithDup([1, 2, 3])) === JSON.stringify([[], [1], [2], [3], [1, 2], [1, 3], [2, 3], [1, 2, 3]]));
{'fraction_non_alphanumeric': 0.18685888550041585, 'fraction_numerical': 0.03631827003049626, 'mean_word_length': 2.176056338028169, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3773303', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1241, 'n_words': 325}