JAVA Self Explanatory O(n) Easy
\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int res=0;\n        boolean[] visited = new boolean[nums.length];\n        for(int i=0;i<
```\nclass Solution {\n    public int arrayNesting(int[] nums) {\n        int res=0;\n        boolean[] visited = new boolean[nums.length];\n        for(int i=0;i<nums.length;i++){\n            if(!visited[i]){\n                int len = dfs(nums,i,visited);\n                res = Math.max(res,len);\n            }\n        }\n        return res;\n    }\n    public int dfs(int[] nums,int i,boolean[] visited){\n        if(visited[i]) return 0;\n        visited[i] = true;\n        return 1+dfs(nums,nums[i],visited);\n    }\n}\n```
['Depth-First Search', 'Java']