Skip to content

Instantly share code, notes, and snippets.

@jjasonclark
Created November 11, 2011 18:10
Show Gist options
  • Save jjasonclark/1358725 to your computer and use it in GitHub Desktop.
Save jjasonclark/1358725 to your computer and use it in GitHub Desktop.
Find ranges in collection of ints
using System;
using System.Collections.Generic;
using System.Linq;
namespace Program
{
internal class RangeFinder
{
public static IEnumerable<T> Create<T>(IEnumerable<int> items, Func<int, int, T> rangeCreator)
{
return items
.OrderBy(v => v)
.Select((v, i) => new { value = v, group = v - i })
.GroupBy(b => b.group, b => b.value, (key,group) => rangeCreator(group.First(), group.Last()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment