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 ?

Junaid
  • 2,572
  • 6
  • 41
  • 77

1 Answers1

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.

Community
  • 1
  • 1
specializt
  • 1,913
  • 15
  • 26
  • 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. – Junaid Feb 05 '15 at 20:38
  • 1
    certainly - this is java, after all : http://developer.classpath.org/doc/java/util/Collections-source.html – specializt Feb 05 '15 at 20:40
  • 1
    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. – Pshemo Feb 05 '15 at 20:40
  • i edited my comment - the first link was about old versions of java, apparently. – specializt Feb 05 '15 at 20:42