5

I'm trying to write a function that can perform permutation.

For example, if I input [1, 2, 3], the expected answer will be

[ [ 3, 2, 1 ], [ 3, 2, 1 ],[ 3, 2, 1 ],[ 3, 2, 1 ],[ 3, 2, 1 ],[ 3, 2, 1 ] ]

But instead of showing the answer, it returns [[ ],[ ],[ ],[ ],[ ]]

Any ideas?

var permute = (nums) => {
    results = [];

    var backtrack = (nums, result) => {
        if (nums.length === result.length) {
            results.push(result);
        } else {

            for (var i = 0; i < nums.length; i++) {
                if (result.indexOf(nums[i]) > -1) {
                    continue;
                }
                result.push(nums[i]);
                backtrack(nums, result);
                result.pop();
            }
        }
    }
    backtrack(nums, []);
    return results;

};

console.log(permute([1, 2, 3]));

2 Answers 2

5

You could take a local copy of result by slicing this array to prevent the same object reference in the result set.

var permute = (nums) => {
    var results = [];
    var backtrack = (nums, result) => {
        if (nums.length === result.length) {
            results.push(result.slice());           // push copy
        } else {
            for (var i = 0; i < nums.length; i++) {
                if (result.indexOf(nums[i]) > -1) {
                    continue;
                }
                result.push(nums[i]);
                backtrack(nums, result);
                result.pop();
            }
        }
    };
  
    backtrack(nums, []);
    return results;
};

console.log(permute([1, 2, 3]).map(a => a.join(' ')));

A version without pushing and popping.

var permute = (nums) => {
    var results = [];
    var backtrack = (nums, result) => {
        if (nums.length === result.length) {
            results.push(result);
        } else {
            for (var i = 0; i < nums.length; i++) {
                if (result.indexOf(nums[i]) > -1) {
                    continue;
                }
                backtrack(nums, result.concat(nums[i])); // use a new array
            }
        }
    };
  
    backtrack(nums, []);
    return results;
};

console.log(permute([1, 2, 3]).map(a => a.join(' ')));

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! So this is due to object & array in js are pass by reference; therefore, the references in results become empty after the function is complete right ?
right. otoh you need this reference to manipulate with push and pop, but only inside of the actual function. it yould work if you replace result.push(nums[i]); backtrack(nums, result); result.pop(); just with backtrack(nums, result.concat(nums[i]));
Wow, never thought of that. Thanks buddy ! :)
0

Just Another approach using reverse(), an special case of permutation which OP gives as an example:

var arr = [ [ 3, 2, 1 ], [ 3, 2, 1 ],[ 3, 2, 1 ],[ 3, 2, 1 ],[ 3, 2, 1 ],[ 3, 2, 1 ] ];

var permuted = arr.map((num) => {

    return num.reverse();

});

console.log(permuted);

2 Comments

how does reversing generate a permutation of the data?
The expected response given by OP is a reverse. It's an special case of permutation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.