Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

219
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda