Finding Unique Values and Summing them from an Array Using LINQ

Assume I have an array ArrayVariable = New System.Int32(){1,1,2,2,3,4,4,5}
And I wish to sum the distinct values into Sum.

I know how to find distinct values, but I am not able to sum them

Any idea?

Thanks in Advance!

-Ashwin A.K

@ashwin.ashok

Check as below

Use ArrayVariable.Distinct().ToArray.Sum(Function (r) Double.Parse(r.ToString))

Mark as solution if this help you

Thanks

3 Likes

Brilliant!
The highlighted operation in
ArrayVariable.Distinct.ToArray.Sum(Function(s) s)
didn’t show up in intellisense, so I thought it wouldnt work.
Thank you once again!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.