-1

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);
DevProg
  • 39
  • 6
  • Does this imply that `array1` has 10 more elements than `array2`? And that the first 10 elements of `array1` don't matter? – BJ Myers Dec 09 '15 at 21:25
  • No, just that each element in array1 is 10 more than each element in array2 so if array1[0]=10 then array2[0]=20 and so on – DevProg Dec 09 '15 at 21:29
  • Oh, then your statement is wrong. It should be `array1[i] + 10 == array2[i]`. – BJ Myers Dec 09 '15 at 21:31
  • @DevProg That's _very_ different that what you've tried. You want `array1[i] + 10 =array2[i]` instead. – D Stanley Dec 09 '15 at 21:31
  • Sorry, that's what I meant – DevProg Dec 09 '15 at 21:32
  • @DevProg What did you try to do to accomplish this goal, and what problem(s) did you have with your attempted solutions? – Servy Dec 09 '15 at 21:33
  • I tried using for loops. However, the problem is what I want to execute a specific statement once if ALL runs of the for loop are true. As such, I do not wish to place the statement in the body of the for loop as it will run multiple times – DevProg Dec 09 '15 at 21:36
  • 1
    So put the statement _outside_ of the for loop. When you ask incomplete questions you get incomplete answers... – D Stanley Dec 09 '15 at 21:39
  • refer http://stackoverflow.com/questions/713341/comparing-arrays-in-c-sharp – Vasistan Dec 10 '15 at 10:21

2 Answers2

0

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);
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

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.

BJ Myers
  • 6,617
  • 6
  • 34
  • 50
  • Thanks BJ. However, I am getting the following error: The type arguments for method 'Enumerable.SequenceEqual(IEnumerable, IEnumerable)' cannot be inferred from the usage. Try specifying the type arguments explicitly. – DevProg Dec 09 '15 at 21:44
  • The arrays are byte arrays – DevProg Dec 09 '15 at 21:45
  • For a byte array, modify the `Select` clause to `n => (byte)(n + 10)`. – BJ Myers Dec 09 '15 at 21:56