0

I have two list like this:

a1 = { 1, 2, 3, 4, 5};
a2 = { 1, 3};

I want to check whether a2 is a consecutive subset of a1. I use:

bool isSubset = !a2.Except(a1).Any();

Based on that example, isSubset should be false, but when I run, isSubset = true.

  • 1
    A set is unordered, so it is not what you are looking for. What you are looking for is basically the same as searching for a substring in a string. Look into string matching algorithms, that should give you an idea how to implement it. – Sentry Apr 29 '17 at 13:46

1 Answers1

0

Could this ever happen in your system:

a1 = { 1, 2, 3, 4, 1, 3, 5};
a2 = { 1, 3};

Or is this not possible?

Also look at this:

Finding a subsequence in longer sequence

also, F# would be a much neater language for solving this kind of problem.

Community
  • 1
  • 1
Hugh
  • 748
  • 9
  • 9
  • each of list is unique, so that condition is not possible. Now I already can solve that problem, but my another problem is how to know the number. i.e. based on that example, subset = {1,3} – Arlita Nurmaya Asri May 01 '17 at 10:02