Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

222
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda