Doubt in programing question

i am having NosuchElementException
kindly go through my code and guide me how to cure it , also tell why it occurs

import java.util.*;

public class SumOfLength {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=scanner.nextInt();
}
int i=0;
int j=0;
int sum=0;
HashMap<Integer,Integer> map=new HashMap<>();
for(j=0;j<n;)
{
if(map.containsKey(arr[j]))
{
map.remove(arr[i]);
i++;
continue;
}
else
{
map.put(arr[j],0);
int x=((j-i+1)*(j-i+2))/2;
sum=sum+x;
j++;
}
}
System.out.println(sum);
}

}