Only one of three test case is runnin fine what is the error please provide me the corrected code

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i= 0;i<n;i++){
arr[i] = sc.nextInt();
}
int x = sumoflength(arr,n);
System.out.print(x);
}
public static int sumoflength(int[] arr, int n)
{

    // For maintaining distinct elements. 
    Set<Integer> s = new HashSet<>(); 

    // Initialize ending point and result 
    int j = 0, ans = 0; 

    // Fix starting point 
    for (int i = 0; i < n; i++) 
    { 
        while (j < n && !s.contains(arr[j])) 
        { 
            s.add(arr[i]); 
            j++; 
        } 

        // Calculating and adding all possible length 
        // subarrays in arr[i..j] 
        ans += ((j - i) * (j - i + 1)) / 2; 

        // Remove arr[i] as we pick new stating point 
        // from next 
        s.remove(arr[i]); 
    } 

    return ans; 
} 

}

please look into chat for the solution. thanks.

you need to check for the cases like 1 2 3 2… where you need to check for all the elements added so far in your hashset not only for starting element… detailed discussion is in chat