public static IEnumerable <NumberIndexRange> GetIndexRanges(int[] set) { var numbers = new HashSet <int>(); NumberIndexRange currentNumber = null; int start = 0; for (int i = 0; i < set.Length; i++) { var n = set[i]; if (!numbers.Contains(n)) { numbers.Add(n); if (currentNumber != null) { currentNumber.Range = i - start; yield return(currentNumber); } start = i; currentNumber = new NumberIndexRange { Number = n }; } } currentNumber.Range = set.Length - start; yield return(currentNumber); }
public static IEnumerable<NumberIndexRange> GetIndexRanges(int[] set) { var numbers = new HashSet<int>(); NumberIndexRange currentNumber = null; int start = 0; for (int i = 0; i < set.Length; i++) { var n = set[i]; if (!numbers.Contains(n)) { numbers.Add(n); if (currentNumber != null) { currentNumber.Range = i - start; yield return currentNumber; } start = i; currentNumber = new NumberIndexRange { Number = n }; } } currentNumber.Range = set.Length - start; yield return currentNumber; }