2

I have a question, I need to check if some array is part of greater array, it would be rather easy but I need to check if the greater array contains exact same sequence. For example

int[] greaterArray = {8, 3, 4, 5, 9, 12, 6 ... n - elements}
int[] lesserArray = {3, 4, 5}

Now I need to know if lesser array is part of this array but with same sequence so It it contains 3, 4, 5 next to each other in greater array.

I tried:

var exists = greaterArray.Intersect(lesserArray).Any();

But it return me information if any element of lesser array exists in greater array, not exact sequence. Any ideas?

User_Targaryen
  • 4,125
  • 4
  • 30
  • 51
Wojciech Szabowicz
  • 3,646
  • 5
  • 43
  • 87
  • 1
    http://stackoverflow.com/documentation/c%23/1429/arrays/16892/comparing-arrays-for-equality#t=201610260846138188674 – Jaydip Jadhav Oct 26 '16 at 08:47
  • 2
    @JaydipJ That answer does not refer to subsets. – Steve Oct 26 '16 at 08:57
  • This is just ans to what you stated in your Question Header *Check if array contains exact same sequence as other array* – Jaydip Jadhav Oct 26 '16 at 08:59
  • Not my question header. Still, the fact remains; it doesn't answer the question. – Steve Oct 26 '16 at 09:00
  • Absolutely not @JaydipJ. He's asking about subsequence not the same array – Tinwor Oct 26 '16 at 09:06
  • Following the suggestion from JaydipJ, http://stackoverflow.com/documentation/c%23/1429/arrays/9911/checking-if-one-array-contains-another-array#t=201610261005285063006 – grek40 Oct 26 '16 at 10:08

5 Answers5

4
    int[] greaterArray = {8, 3, 4, 5, 9, 12, 6};
    int[] lesserArray = { 3, 4, 5 };
    bool sequenceFound = false;

    for (int i = 0; i <= greaterArray.Length - lesserArray.Length; i++)
    {
        if (greaterArray.Skip(i).Take(lesserArray.Length).SequenceEqual(lesserArray))
        {
            sequenceFound = true;
            break;
        }
    }

    if (sequenceFound)
    {
        //sequence found
    }
    else
    {
        //sequence not found
    }

Use the above code. It takes multiple sub-sequences from greaterArray of length equal to the length of lesserArray and matches it with lesserArray.

Akshey Bhat
  • 8,227
  • 1
  • 20
  • 20
0

A bit more generic and without the use of LINQ:

int[] greaterArray = {8, 2, 4, 5, 9, 12, 3, 4, 5};
int[] lesserArray = {3, 4, 5};
for (int i = 0; i <= greaterArray.Length - lesserArray.Length; i++)
            {
                var sub = greaterArray.SubArray(i, lesserArray.Length);
                if (Enumerable.SequenceEqual(sub, lesserArray))
                {
                    Console.WriteLine("Equals!");
                }
            }

And this utils to get the SubArray:

public static T[] SubArray<T>(this T[] data, int index, int length)
        {
            T[] result = new T[length];
            Array.Copy(data, index, result, 0, length);
            return result;
        }
Tinwor
  • 7,765
  • 6
  • 35
  • 56
0

This should do your work

int[] grtarr = { 8, 3, 4, 5, 9, 12, 6 };
int[] lsarr = { 3, 4, 5 };

List<int> lstGrtArr = grtarr.ToList();
List<int> lstLsrArr = lsarr.ToList();

bool sequenceMatch = false;

for (int i = 0; i < grtarr.Count(); i++)
{
    if (lstGrtArr.Where(x => lstGrtArr.IndexOf(x) >= i).Take(lstLsrArr.Count()).SequenceEqual(lstLsrArr))
    {
        sequenceMatch = true;
        break;
    }
}

if(sequenceMatch)
{
    //Do Something
}
Amey Kamat
  • 181
  • 2
  • 12
  • 1
    http://stackoverflow.com/questions/18037625/check-if-one-list-contains-all-items-from-another-list-in-order – Vivek Nuna Oct 26 '16 at 09:25
0
    static bool isPrefix(int[] source, int start_pos, int[] prefix)
    {
        bool result = start_pos + prefix.Length <= source.Length;
        for (int i = 0; result && i < prefix.Length; ++i, ++start_pos)
            result = source[start_pos] == prefix[i];
        return result;
    }
    static bool Contains(int[] source, int[] prefix)
    {
        bool result = false;
        for (int i = 0; !result && i < source.Length; ++i)
            result = source[i] == prefix[0] ? isPrefix(source, i, prefix) : false;
        return result;
    }
Alex Aparin
  • 4,393
  • 5
  • 25
  • 51
-1

Use this piece of code:

public bool ArraysEqual<T>(T[] a1, T[] a2)
{
    if (ReferenceEquals(a1,a2))
        return true;

    if (a1 == null || a2 == null)
        return false;

    if (a1.Length != a2.Length)
        return false;

    EqualityComparer<T> comparer = EqualityComparer<T>.Default;
    for (int i = 0; i < a1.Length; i++)
    {
        if (!comparer.Equals(a1[i], a2[i])) return false;
    }
    return true;
}

Or if you want to use Linq and don't care too much about performance, the easiest thing is:

var arrays_are_the_same = Enumerable.SequenceEqual(a1, a2);
Dawid Rutkowski
  • 2,658
  • 1
  • 29
  • 36
  • How does performance differ between your 2 solutions? Its not like your `ArraysEqual` contains some speedup magic (not my -1 though). – grek40 Oct 26 '16 at 08:51
  • This doesn't answer the question. The OP is asking if the `lesserArray` is a subset of `greaterArray` with the elements being in the same sequence order. – Steve Oct 26 '16 at 08:52