C# Array Subset fetching


C# Array Subset fetching



I have an array of bytes and i want to determine if the contents of this array of bytes exists within another larger array as a continuous sequence. What is the simplest way to go about doing this?




Good datastructure for look up of an id mapping to set of elements (c++)

1:



If you had an array of Strings, what is the quickest way to sort this array in ascending order?
The naive approach is:.
How to automatically generate a sports league schedule
public static bool IsSubsetOf(byte[] set, byte[] subset) {     for(int i = 0; i < set.Length && i + subset.Length <= set.Length; ++i)         if (set.Skip(i).Take(subset.Length).SequenceEqual(subset))             return true;     return false; } 
For more efficient approaches, you might consider more advanced string matching algorithms like KMP..
Javascript Fibonacci nth Term Optimization


Efficient way to walk through an object tree?


Can this checksum algorithm be improved?

2:



difference between 2 datatables
Try to adapt some string search algorithm.


LinkedIn type friends connection required in php
One of the fastest is Boyer-Moore .

It's quite easy as well.

For binary data, Knuth-Morris-Pratt
algorithm might work very efficiently as well..


3:


This, which is a 1/1 port of this answer: http://stackoverflow.com/questions/1507780/searching-for-a-sequence-of-bytes-in-a-binary-file-with-java . Is a very efficient way of doing so:.
public static class KmpSearch {      public static int IndexOf(byte[] data, byte[] pattern) {         int[] failure = ComputeFailure(pattern);          int j = 0;         if (data.Length == 0) return -1;          for (int i = 0; i < data.Length; i++) {             while (j > 0 && pattern[j] != data[i]) {                 j = failure[j - 1];             }             if (pattern[j] == data[i]) { j++; }             if (j == pattern.Length) {                 return i - pattern.Length + 1;             }         }         return -1;     }       private static int[] ComputeFailure(byte[] pattern) {         int[] failure = new int[pattern.Length];          int j = 0;         for (int i = 1; i < pattern.Length; i++) {             while (j > 0 && pattern[j] != pattern[i]) {                 j = failure[j - 1];             }             if (pattern[j] == pattern[i]) {                 j++;             }             failure[i] = j;         }          return failure;     } } 



76 out of 100 based on 76 user ratings 1126 reviews