I have a Column class which has an Index property of type int.
If I have a collection of Column objects, I am looking for a way to test if their indices are contiguous. By contiguous I mean that the indices are next to each other, so if ordered by value they are 1 apart from the next and previous Index.
There can be any number of column objects.
So, for example:
10,11,12,13 => true
3,5,7 => false
1,2,4 => false
While these examples are of ordered indices, I would like a solution that takes an unordered set of indices.
I feel sure there is probably a neat Linq way of solving this, but I cannot see it.
Expressed in code:
public class Column
{
public int Index { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Example set of columns 1
List<Column> columns1 = new List<Column>()
{
new Column(){Index = 10},
new Column(){Index = 11},
new Column(){Index = 12},
new Column(){Index = 13},
};
// Example set of columns 2
List<Column> columns2 = new List<Column>()
{
new Column(){Index = 3},
new Column(){Index = 5},
new Column(){Index = 7},
};
// Example set of columns 3
List<Column> columns3 = new List<Column>()
{
new Column(){Index = 1},
new Column(){Index = 2},
new Column(){Index = 4},
};
var result1 = IndicesAreContiguos(columns1); // => true
var result2 = IndicesAreContiguos(columns2); // => false
var result3 = IndicesAreContiguos(columns3); // => false
}
public bool IndicesAreContiguos(IEnumerable<Column> columns)
{
// ....???
}
}
You don't need LINQ for this
public bool IsContig(int[] arr) {
for(int i = 1; i<arr.Length;i++)
if(arr[i] - arr[i-1] != 1)
return false;
return true;
}
LINQ is a hammer, but not every problem is a nail
(Edit: to accept an unordered set of indices, then consider a modification that sorts the array first. Again, LINQ isn't necessary; Array.Sort would work)
If a sequence of 1,2,3,2,3,2,3,4,3,2,3,4,5,4,5 is contiguous, improve the IF to allow for a result of -1 too
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With