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

284
Visualizações
How can I mimic a Javascript "spread operator" in C#?

I am following a tutorial on Dynamic Programming on youtube to understand more about Recursive functions, and I am stuck where a spread operator is used.

Code in JavaScript

const howSum = (targetSum, numbers) =>{
    if(targetSum === 0) return [];
    if(targetSum < 0) return null;

    for(let num of numbers){
           const remainder = targetSum - num;
           const remainderResult = howSum(remainder, numbers);
            if(remainderResult != null){
                 return [...remainderResult, num];
            } 
    }
    return null;

};

This is the code in C# where I'm trying to replicate the function

class HowSumSlow {

    static dynamic HowSum(int targetSum, int[] numbers)
    {
            
        if (targetSum == 0) return numbers;
        if (targetSum < 0) return null;

        
        foreach( var num in numbers){
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null) { 
                //Code here//
            }
        }
        return null;
    }

    static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3 };
        Console.WriteLine(HowSum(7, numbers));
    }
    
}

EDIT: Should I use a Dictionary and use a key? I don't understand how to work my way around this one.

  static Dictionary<int, int[]> spread = new Dictionary<int, int[]>();
            
        static dynamic HowSum(int targetSum, int[] numbers){
        ...
            if(spread.ContainsKey(remainderResult)){
                return spread[remainderResult];
        }
    }

EDIT:

class HowSumSlow {

    static int[] HowSum(int targetSum, int[] numbers)
    {
        int[] empty = new int[] { };
        if (targetSum == 0) return empty;
        if (targetSum < 0) return null;

        
        
        foreach( var num in numbers){
            var remainder = targetSum - num;
            int[] remainderResult = HowSum(remainder, numbers);

            if (remainderResult != null){
                return remainderResult.Append(num).ToArray();
            }
        }
        return null;
    }
    static void Main(string[] arg) {
        int[] numbers = new int[] { 2, 3, 5 };
        Console.WriteLine(String.Join(",", HowSum(8, numbers)));
    }


}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is no spread operator in c#, you could use the Append method in the System.Linq namespace. As this returns an IEnumerable<T> you'll also need to call ToArray() afterwards.

This line in JS

return [...remainderResult, num];

Could be the following in c#

return remainderResult.Append(num).ToArray();

Note that your method always returns int[] or null, so the return type should be int[] not dynamic!

about 4 years ago · Juan Pablo Isaza Relatório
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