Grouping neighboring values โ€‹โ€‹with linq

I have a sorted list of int values, and I want to make groups adjacent values. Values โ€‹โ€‹where the next value is nextvalue> = prevValue + 1 are neighbors.

For example: List:

{1,2,3,5,6,8,9,10} 

Groups will be:

 {1,2,3} {5,6} {8,9,10} 

Can this be done with linq?

This can easily be done without linq - by iterating through the list, but I am wondering if linq has a solution for this.

+5
source share
4 answers

This worked for me:

 private static void GroupToNeighboursTest() { var numbers = new List<int> { 1, 2, 3, 5, 6, 8, 9, 10 }; var neighbours = numbers .Zip(numbers.Skip(1), Tuple.Create) .Aggregate(new List<List<int>> { new List<int> { numbers.First() } }, (result, i) => { if (i.Item1 == i.Item2 - 1) { result.Last().Add(i.Item2); } else { result.Add(new List<int> { }); result.Last().Add(i.Item2); } return result; }); } 

enter image description here

+1
source

If you must use Linq, you can consider the Aggregate method.

 x.Aggregate(new List<List<int>> { new List<int>() }, (soFar, next) => { if (soFar.Last().Contains(next - 1)) { soFar.Last().Add(next); } else { soFar.Add(new List<int> { next }); } return soFar; }); 
+1
source

This works for me:

 var result = items .Skip(1) .Aggregate( new [] { items.Take(1).ToList() }.ToList(), (acc, item) => { if (acc.Last().Last() == item - 1) { acc.Last().Add(item); } else { acc.Add(new [] { item }.ToList()); } return acc; }); 

I get this result:

result

As a side note, I like to get into the habit of creating lists like this new [] { item }.ToList() , because it allows you to create lists of anonymous types. Very useful in many LINQ queries.

+1
source

I only have a non-linq solution for this (if I understood the problem correctly). Hope this helps.

 void Main() { var list = new List<int>{1,2,3,5,6,8,9,10}; GetData(list).Dump(); } public IEnumerable<List<int>> GetData(IEnumerable<int> data) { int prev = 0; var g = new List<int>(); foreach (var item in data) { if(item - prev > 1 && g.Count > 0) { yield return g; g = new List<int>(); } g.Add(item); prev = item; } yield return g; } 
0
source

Source: https://habr.com/ru/post/1208279/


All Articles