There is an array, for example:
var x = new [] {1,2,3,4,5,6,7,8}; It is necessary to find the difference between adjacent elements, respectively:
{1,2,3,4,5,6,7,8} -> {1,1,1,1,1,1,1} How can this be implemented by regular means?
For example, like this: x.Zip(x.Skip(1), (first, second) => second - first) .
Another option using Aggregate
var result = x.Skip(1).Aggregate(new { prev = x[0], result = Enumerable.Empty<int>() }, (acc, cur) => new { prev = cur, result = acc.result.Concat(new[] { cur - acc.prev }) }, a => a.result.ToArray() ); Source: https://ru.stackoverflow.com/questions/516057/
All Articles