Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
What is the difference between passing by reference and passing by value of arrays for recursive calls which share a reference via a closure?

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!