How can I compare two arrays such that:
array1[i+10]==array2[i]
Is always true?
I know the following can compare arrays, but what about this specific case?
bool isEqual = Enumerable.SequenceEqual(array1, array2);
How can I compare two arrays such that:
array1[i+10]==array2[i]
Is always true?
I know the following can compare arrays, but what about this specific case?
bool isEqual = Enumerable.SequenceEqual(array1, array2);
The old-fashioned way:
int length = array2.Length;
bool areEqual = true;
for(int i = 0; i < length; i++)
{
if(array1[i] + 10 = array2[i])
{
areEqual = false;
break;
}
}
The linq way:
var query1 = array1.Select(i => i + 10);
bool areEqual = Enumerable.SequenceEqual(query1,array2);
The answer differs depending on what you mean by array1[i+10]==array2[i]
.
If the values of array2
should be 10 more than the values of array1
:
Use Select
to create a temporary array that adds 10 to each value in array1
, then compare to array2
:
bool equal2 = Enumerable.SequenceEqual(array1.Select(n => n + 10), array2);
If the values of array2
should be the same as array1
, shifted over 10 places:
Use Skip
to jump past the first 10 elements of array1
, then compare to array2
:
bool equal = Enumerable.SequenceEqual(array1.Skip(10), array2);
Note that this only works if array1
has exactly 10 more elements than array2
.