1

Is there a way to find an array within another array like

a=[1,2,3,4,5,6,7]
b=[2,3,4]
c=[2,4,5]

// b is child of a, but c is NOT child of a.

Well I know that using Brute-force approach I can find the array within another array. But I want to know that is there any algo that can help me ... or (as I am using JAVA so) is there any built-in feature in JAVA that can help me ?

1
  • 2
    what is "bootforce" supposed to mean? Commented Feb 5, 2015 at 20:32

1 Answer 1

6

As already mentioned here :

https://stackoverflow.com/a/3940684/351861:

public static int findArray(Integer[] array, Integer[] subArray)
{
    return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}

Java has builting features for that, apparently.

Sign up to request clarification or add additional context in comments.

4 Comments

Good ! Thanks. Just curious ... can you please let me know that what algo is behind the Collections.indexOfSubList() ... so that I can have good idea about this function's algorithm - as I am much concerned about algorithms.
Consider adding to answer fact that it will not work for arrays of primitive types like int[]. We would need to use Integer[] instead because of how generics works.
i edited my comment - the first link was about old versions of java, apparently.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.