Doubt regarding Time analysis

we have used 2 loops to calculate the length of the non repetitive substrings then how could the complexity be in O(n) instead of O(n^2). Please Elaborate on this.

try to dry run this loop

for(int i=0;i<n;i++){
        while(j<n && s.find(arr[j])==s.end()){
           s.insert(arr[j]);
            j++;
        }
        ans+=(j-i) *(j-i+1)/2;
        s.erase(arr[i]);
    }

this will never go to n^2 as we have another condition along with j<n
debug this