1

I was working on a solution to the following prompt: Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

The following code passes the tests:

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
const permute = function(nums) {
  const results = [];
  const backtrack = (first = 0) => {
    if (first === nums.length) {
      results.push([...nums]);
      return
    }
    for (let i = first; i < nums.length; i++) {
      nums = swap(nums, first, i);
      backtrack(first + 1);
      nums = swap(nums, first, i)
    }
  }
  backtrack()
  return results
};

const swap = (arr, i, j) => {
  const temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
  return arr;
}

Consider an example for calculating the permutations where nums = [1, 2, 3].

The gap in my understanding is apparent when I try to pass in nums to results.push() instead of results.push([...nums]), which returns the appropriate length result array, but is filled with [1, 2, 3] array at each index. Why is it necessary to make a shallow copy of nums here instead of just pushing nums directly? My understanding is that at the point in the recursive call where the base case is satisfied, nums will be whatever the current permutation is. Following the storing of our permutation, we backtrack/unswap nums and continue. I suppose it must be the case that all permutations in the result array will always reflect the current value of the reference element, but if that is the case, why is the final result array then filled with [1, 2, 3] for each element?

I thought I previously had a strong understanding of passing by reference vs passing by value but now I am doubting that understanding. Any clarification on this would be greatly appreciated as I feel like I am almost there but can't quite get it to click.

Thanks for taking the time to read/help.

Leo C.
  • 11
  • 1
  • 2
  • Does your code work? We fix code: [ask] post code: [mcve] – zer00ne Jan 18 '22 at 19:35
  • If you don't make a copy, then all the elements of `results` will be the same array, and when you swap elements it will affect all of them the same. – Barmar Jan 18 '22 at 19:36
  • 1
    I don't see what this has to do with call-by-reference vs call-by-value. There is only **one** array `nums` and another array `results` in your code (if not making the copy). – Bergi Jan 18 '22 at 20:57
  • possible duplicate of [Copy array by value](https://stackoverflow.com/q/7486085/1048572) – Bergi Jan 18 '22 at 20:58

0 Answers0