Just change the visited value to negative. Once we get the negative value, stop the while loop and compare the result with max.

```
public class Solution {
public int arrayNesting(int[] nums) {
int max=0;
for(int i=0; i<nums.length;i++){
int j=i,count=0;
while(nums[j]!=-1){
int temp=j;
j=nums[j];
nums[temp]=-1;
count++;
}
if(count>max) max=count;
}
return max;
}
}
```