Find the first occurrence/starting index of the sub-array in C#

Simplest to write? return (from i in Enumerable.Range(0, 1 + x.Length – y.Length) where x.Skip(i).Take(y.Length).SequenceEqual(y) select (int?)i).FirstOrDefault().GetValueOrDefault(-1); Not quite as efficient, of course… a bit more like it: private static bool IsSubArrayEqual(int[] x, int[] y, int start) { for (int i = 0; i < y.Length; i++) { if (x[start++] != y[i]) return false; } … Read more