Find list in another list

vacip

Is there an easy way to find one list in another, taking the order into consideration too? (Besides looping through them.) Basically like the way String's .IndexOf works.

string[] lookInThis = { "a", "b", "c", "d" };
string[] lookForThis1 = { "b", "c" };
string[] lookForThis2 = { "b", "d" };

int FoundLoc = string.Join(",", lookInThis).IndexOf(string.Join(",", lookForThis1));

This works for my strings, but feels like it can be improved.

In my example, these are my expected outputs:

lookForThis1   1
lookForThis2  -1 or something like that.
Knells

This should do what you're asking. Not exactly pretty as I just threw it together and I'm no LINQ genie:

    public int SublistIndex(string[] lookInThis, string[]lookForThis)
    {
        int i;
        for (i = 0; i < lookInThis.Count(); i++)
        {
            if (lookInThis.ElementAt(i).Equals(lookForThis.First()))
            {
                //Found the first element of the list we are searching for
                int j;

                //Now we need to check if the other elements are there in correct order
                for (j = 0; j < lookForThis.Count(); j++)
                {
                    if (i + j == lookInThis.Count())
                    {
                        //Reached the end of the lookInThis list with no match
                        return -1;
                    }
                    if (!lookInThis.ElementAt(i + j).Equals(lookForThis.ElementAt(j)))
                    {
                        //Sequence is not identical, stop inner loop
                        break;
                    }
                }
                if (j == lookForThis.Count())
                {
                    //found it!
                    return i;
                }
            }
        }
        //reached the end and didn't find it
        return -1;
    }

Tested with this:

        string[] t1 = { "a", "b", "c" };
        string[] t2 = { "b", "c" };
        string[] t3 = { "b", "d" };
        int tt1 = SublistIndex(t1, t2);
        int tt2 = SublistIndex(t1, t3);

tt1 = 1 and tt2=-1

You can substitute string for any type basically, provided you also change the .equals() comparison to an appropriate one.

How it works:

It loops through lookInThis and when finding the starting element of lookForThis it starts another loop to compare them. If it finds any elements don't match it breaks this loop an resumes. If it reaches the end of lookForThis it returns the index of the first loop. It returns -1 when it reaches the end of lookInThis. Quick and dirty, so probably not advised for huge lists.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to find if a list contains any element of another list in sqlite

From Java

How do I find the duplicates in a list and create another list with them?

From Dev

Find out if string list items startswith another item from another list

From Dev

Sort list of dictionaries by another list

From Dev

Pythonic way to find elementa of a python list that are not contained in another python list

From Dev

Find and replace matching multiple parameters from list (or another file)

From Dev

Sort List by another, incomplete list

From Dev

How to iterate and sum each element in a list with another to find a number?

From Dev

How to find words from list of words in another list

From Dev

how to find elements from a list that are not present in another list in r

From Dev

Find and replace each instance of a single string with another from a list or file

From Dev

Find elements in a list of which all elements in another list are factors, using a list comprehension

From Dev

List containing another list

From Dev

Find if a String in a list of Strings is in another list of Strings in Esper

From Dev

Can find be used to find a list in another list?

From Dev

Find the minimum value of a list and print the corresponding index from another list

From Dev

Find out if string list items startswith another item from another list

From Dev

find if any element of list is in another list

From Dev

Python - Find suffixes that commonly occur in one list, but not in another

From Dev

find and get + set item value to another Iqueryable list of same type

From Dev

Efficiently find all elements of a List<string> that start with another string

From Dev

How to find a list of steps needed to reorder a list to get another list?

From Dev

How to find the index of last element on list and add it to another

From Dev

Find a list of files that contains a set of consecutive lines defined in another file

From Dev

Sorting a list into another list

From Dev

Find strings from a list starting with strings in another list

From Dev

find lists that start with items from another list

From Dev

Using mysql, find a list of IDs that have 1 value but not another

From Dev

find index of element corresponding to another list

Related Related

  1. 1

    How to find if a list contains any element of another list in sqlite

  2. 2

    How do I find the duplicates in a list and create another list with them?

  3. 3

    Find out if string list items startswith another item from another list

  4. 4

    Sort list of dictionaries by another list

  5. 5

    Pythonic way to find elementa of a python list that are not contained in another python list

  6. 6

    Find and replace matching multiple parameters from list (or another file)

  7. 7

    Sort List by another, incomplete list

  8. 8

    How to iterate and sum each element in a list with another to find a number?

  9. 9

    How to find words from list of words in another list

  10. 10

    how to find elements from a list that are not present in another list in r

  11. 11

    Find and replace each instance of a single string with another from a list or file

  12. 12

    Find elements in a list of which all elements in another list are factors, using a list comprehension

  13. 13

    List containing another list

  14. 14

    Find if a String in a list of Strings is in another list of Strings in Esper

  15. 15

    Can find be used to find a list in another list?

  16. 16

    Find the minimum value of a list and print the corresponding index from another list

  17. 17

    Find out if string list items startswith another item from another list

  18. 18

    find if any element of list is in another list

  19. 19

    Python - Find suffixes that commonly occur in one list, but not in another

  20. 20

    find and get + set item value to another Iqueryable list of same type

  21. 21

    Efficiently find all elements of a List<string> that start with another string

  22. 22

    How to find a list of steps needed to reorder a list to get another list?

  23. 23

    How to find the index of last element on list and add it to another

  24. 24

    Find a list of files that contains a set of consecutive lines defined in another file

  25. 25

    Sorting a list into another list

  26. 26

    Find strings from a list starting with strings in another list

  27. 27

    find lists that start with items from another list

  28. 28

    Using mysql, find a list of IDs that have 1 value but not another

  29. 29

    find index of element corresponding to another list

HotTag

Archive