N frames in arraylist

Chef recorded a video explaining his favorite recipe. However, the size of the video is too large to upload on the internet. He wants to compress the video so that it has the minimum size possible.

Chef’s video has N frames initially. The value of the i th
frame is Ai

. Chef can do the following type of operation any number of times:

Choose an index (1≤i≤N) such that the value of the ith
frame is equal to the value of either of its neighbors and remove the i th frame.
Find the minimum number of frames Chef can achieve.
My code is below:

`import java.util.*;

class Codechef
{
public static void main (String[] args)
{
Scanner read = new Scanner(System.in);
int t = read.nextInt();
for(int i=0; i<t; i++)
{
ArrayList a = new ArrayList();
int n = read.nextInt();
for(int j=1; j<=n; j++){
int ele = read.nextInt();
a.add(ele);
}

        if(n==1){
        System.out.println(a.size());
        }
        else if (n>1) {
            for(int j=0;j<a.size()-1;j++){
                if(a.get(j)==a.get(j+1)){
                    a.remove(j);
                    j=-1;
                }
            }
            System.out.println(a.size());
        }
    }    
}

}`
it gives right output for sample test cases and custom cases but not for hidden cases i don’t know that cases exactly where it is not working well.

The below code worked for me for N frames problem.

int cnt=1;
for(int k=0;k<a.size()-1;k++){
if(!a.get(k).equals(a.get(k+1))){
cnt++;
}
}

	    System.out.println(cnt);