0

I want a method that can check if all elements from one list are in another list but in sequential order.

Example

List<int> list1 = new List<int>{ 1, 3, 4 };
List<int> list2 = new List<int>{ 1, 2, 3 };
List<int> list3 = new List<int>{ 1, 2, 3, 4, 5 };

if(IsInSequential(list1, list3)) // should return false
{
    // stuff...
}

if(IsInSequential(list2, list3)) // should return true
{
    // stuff...
}

if(IsInSequential(list3, list2)) // should return false
{
    // stuff...
}

bool IsInSequential(List<int> list1, List<int> list2)
{
    // ???
}

I was trying to use methods like Intersect, Select, Any and other stuff but it was getting too complicated and i don't know if exists a more efficient way.

NioZero
  • 87
  • 2
  • 11
  • Should these start with the same items? – Willem Van Onsem Jul 12 '17 at 21:44
  • In sequential order or in the same order? For example, should `(1, 4, 3), (2, 1, 4, 3, 5)` be true? – Andrew Jul 12 '17 at 21:50
  • Thanks, i was searching and i didn't found anything... – NioZero Jul 12 '17 at 21:59
  • I came with with a simpler solution which can be of use for you: https://stackoverflow.com/a/45068175/2321042 You may want to swap the parameters to match your example. – Andrew Jul 12 '17 at 22:01
  • I changed that answer to use generics, but you can just stick to the simpler `int` version which is in the history. Also remove the `this` and make it private if you just want a simple local method. – Andrew Jul 12 '17 at 22:08

0 Answers0