How to search for elements of sub-array from parent-array, if parent-array have the same elements of sub-array?

Viewed 413

I am new to java. I have a problem that, I have two arrays parentArray and subArray:

  • parentArray has values, {1,4,4,3,6}
  • subArray has values {4,4,3}

So, the second array or so-called subArray values are included in the first-array the so-called parentArray, with indexes starting from [1,2,3]. My question is how can we return the index value of the first element of subArray which is also part of parentArray.

i.e.

int[] parentArray = new int[]{1,4,4,3,6};
int[] subArray = new int[]{4,4,3};

As subArray's values are in parentArray starting index from [1], I want my program to return 1.

I have tried comparing two arrays and returning the common elements from both arrays. But ahead of that, I can not think any of logic as I am a beginner.

3 Answers

you can use the Collections.indexOfSubList() as follows:

List<Integer> parentArray = Arrays.asList(1,4,4,3,6);
List<Integer> subArray = Arrays.asList(4,4,3);

int index = Collections.indexOfSubList(parentArray , subArray);
// index is 1

if you want to implement for arrays, check out the source from that and modify it a bit:

public static int indexOfSubList(int[] source, int[] target) {
  int sourceSize = source.length;
  int targetSize = target.length;
  int maxCandidate = sourceSize - targetSize;

  nextCand:
    for (int candidate = 0; candidate <= maxCandidate; candidate++) {
      for (int i=0, j=candidate; i<targetSize; i++, j++)
        if (!(target[i] == source[j]))
          continue nextCand;  // Element mismatch, try next cand
      return candidate;  // All elements of candidate matched target
    }
  return -1;  // No candidate matched the target
}

usage:

int[] parentArray = new int[]{1,1,1,4,4,3,6};
int[] subArray = new int[]{4,4,3};
int index = indexOfSubList(parentArray, subArray);
// index is 3
System.out.println(index);

I've created a method for this. Try below code,

public int checkSubArray(int[] parentArray, int[] subArray) {
    int result = -1;
    for (int i = 0; i < parentArray.length; i++) {
        int[] temp = Arrays.copyOfRange(parentArray, i, (subArray.length + i)); //This will create a temporary sub array
        if (Arrays.equals(temp, subArray)) {
            System.out.println(i);
            result = i;
        }
    }
    return result;
}

Improved Answer

public int checkSubArray(int[] parentArray, int[] subArray) {
    int result = -1;
    for (int i = 0; i < parentArray.length; i++) {
        if (parentArray[i] == subArray[0]) { // Checking if the first value matches
            int[] temp = Arrays.copyOfRange(parentArray, i, (subArray.length + i)); // This will create a temporary sub array
            if (Arrays.equals(temp, subArray)) {
                System.out.println(i);
                result = i;
            }
        }
    }
    return result;
}

You could also try something like this if your array only contains integers less than 10.

public static void main(String[] args) {

    int[] parentArray = new int[]{1, 4, 4, 3, 6};
    int[] subArray = new int[]{4, 4, 3};

    String s1 = convertToString(parentArray);

    String s2 = convertToString(subArray);

    System.out.print(s1.indexOf(s2));
}

private static String convertToString(int[] array) {

    StringBuilder stringBuilder = new StringBuilder();

    for(int num : array){
        stringBuilder.append(num);
    }
    return stringBuilder.toString();
}
Related