Sum of positive from an array of numbers
The array also includes negative numbers.
Apr 10, 2023
This is an exercise from CodeWars.
You get an array of numbers, return the sum of all of the positives ones.
Example [1,-4,7,12]
=> 1 + 7 + 12 = 20
Note: if there is nothing to sum, the sum is default to 0
.
Here’s my simple solution using Linq.
using System;
using System.Linq;
public class Kata
{
public static int PositiveSum(int[] arr)
{
return arr.Where(x => x > 0).Sum();
}
}