C# Array Subset fetching

后端 未结 3 1752
清酒与你
清酒与你 2021-01-23 00:30

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

3条回答
  •  北海茫月
    2021-01-23 00:51

    The naive approach is:

    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.

提交回复
热议问题