re: Daily Coding Puzzles - Oct 29th - Nov 2nd VIEW POST

re: Thanks for the suggestion there, @asparallel . Here is the updated C# code (with the same logic as JavaScript one) using .Aggregate. using Syst...

Further optimization I'd probably press for, use a tuple to move the entire aggregation onto the stack:

  public static IList<int> CountPositivesSumNegatives(int[] values)
    if (values == null || values.Length == 0) return new int[0];

    var result = values.Aggregate((0, 0), (seed, value) =>
      if (value > 0) seed.Item1++;
      else seed.Item2 += value;
      return seed;

    return new[] { result.Item1, result.Item2 };

Ah ha. Tuple being a value type, it is better optimized.
Thanks for the tips there 👊

code of conduct - report abuse