'check if elements in the fist array are in the same order on another array?

I have a problem I have two arrays let's say arr1=[3, 1 ,6 ,2 ,5 ,8 ,4 ,7 ,9 ,10] and arr2=[9 ,6 ,5, 8, 4, 7, 3 ,1 ,2 ,10] the problem is I want to count how many group of elements were as the same order in both arrays so the output will be ---> 2 since we have "8 4 7" and "3 1" are duplicated with same order this is my code I can't handle the right solution Any help? BTW this is my first post on stack overflow I was stuck for two days if you want more details I can tell you more!

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
//6 7 4 8 1 5 2 9 10 3
// 5 2 6 7 4 8 1 9 10 3
public class Main {
  public static int findIndex(int arr[], int t) {

    // if array is Null
    if (arr == null) {
      return -1;
    }

    // find length of array
    int len = arr.length;
    int i = 0;

    // traverse in the array
    while (i < len) {

      // if the i-th element is t
      // then return the index
      if (arr[i] == t) {
        return i;
      } else {
        i = i + 1;
      }
    }
    return -1;
  }

  public static void main(String[] args) throws Exception {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    int counter = 0;
int counter2=0;
    int n = Integer.parseInt(bufferedReader.readLine().trim());
    int[] arr1 = Arrays.stream(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")).mapToInt(Integer::parseInt)
        .toArray();
    int[] arr2 = Arrays.stream(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")).mapToInt(Integer::parseInt)
        .toArray();
    for (int i = 0; i <arr1.length; i++) {
      if (i < arr1.length - 1) {
        if (findIndex(arr2, arr1[i + 1]) - findIndex(arr2, arr1[i]) == 1) {

          ArrayList<Integer> x = new ArrayList<>();
          if (i >= 1) {
            if (findIndex(arr2, arr1[i + 1]) - findIndex(arr2, arr1[i - 1]) == (i + 1) - (i - 1)) {
              x.add(arr1[i]);
              counter += 1;
              System.out.println(x.toString());
            } 
            else {
              counter2 += 1;
                            System.out.println(x.toString());

            }
          }
        }

      }

    }

    if (Arrays.equals(arr1, arr2)) {
      System.out.println(1);
    } else if (arr1.length == 1) {
      System.out.println(0);
    }
if(counter!=0){
      System.out.println(counter);

}
else{
      System.out.println(counter2+1);

}
  }

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source