Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Excluding elements from array.sum

Tags:

arrays

c#

sum

What is the cleanest way to exclude an element in an array when summing?

Example:

array.sum(exclude index 5);
like image 471
user1429195 Avatar asked Apr 10 '26 13:04

user1429195


1 Answers

Rather than trying to build a temporary array or skip over that value, it might be faster to just subtract the value of array[5] from the sum:

array.Sum() - array[5]
like image 195
Mike Christensen Avatar answered Apr 13 '26 03:04

Mike Christensen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!