C++ O(n) solution with detailed explanation
The key points to this problem:\n\n1. All the numbers in the input array are unique so it doesn\'t exist that two number point to the same index.\n2. Based on a
The key points to this problem:\n\n1. All the numbers in the input array are unique so it doesn\'t exist that two number point to the same index.\n2. Based on above fact,  all sets are totally isolated. I mean one number of a set will never lead us to the number of another set.\n3. Treat each set like a circle so no matter you start with any number of this set, it traverses the whole set.\n\n```\nclass Solution {\npublic:\n    int arrayNesting(vector<int>& nums) {\n        vector<bool> visited(nums.size(), false);\n        int result = 0;\n\t\t//Search for all sets\n        for(int i = 0; i < nums.size(); i++)\n        {\n            int index = i;\n            int count = 0;\n\t\t\t//Count the numbers in one set\n            while(!visited[index])\n            {\n                visited[index] = true;\n                index = nums[index];\n                count++;                \n            }\n\t\t\t//Record the longest set\n            result = max(result, count);\n        }\n        return result;\n    }\n};\n```\n\nPlease upvote if you find my explanation useful.
['C++']