a few solutions
Each candidate path is a cycle.  Traverse each uniquely seen cycle and return the maximum length as the best answer.\n\n---\n\nKotlin\n\nclass Solution {\n    f
Each candidate path is a cycle.  Traverse each uniquely `seen` cycle and return the maximum length as the `best` answer.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun arrayNesting(A: IntArray): Int {\n        var best = 0\n        var seen = mutableSetOf<Int>()\n        for (i in 0 until A.size) {\n            var (cand, j) = listOf(0, i)\n            while (!seen.contains(j)) {\n                seen.add(j); j = A[j]; ++cand\n            }\n            best = Math.max(best, cand)\n        }\n        return best\n    }\n}\n```\n\n*Javascript*\n```\nlet arrayNesting = (A, seen = new Set(), best = 0) => {\n    for (let i = 0; i < A.length; ++i) {\n        let [cand, j] = [0, i];\n        while (!seen.has(j))\n            ++cand, seen.add(j), j = A[j];\n        best = Math.max(best, cand);\n    }\n    return best;\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def arrayNesting(self, A: List[int], best = 0) -> int:\n        seen = set()\n        for i in range(len(A)):\n            cand, j = 0, i\n            while j not in seen:\n                cand += 1; seen.add(j); j = A[j]\n            best = max(best, cand)\n        return best\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    using Set = unordered_set<int>;\n    int arrayNesting(VI& A, Set seen = {}, int best = 0) {\n        for (auto i{ 0 }; i < A.size(); ++i) {\n            auto [cand, j] = make_pair(0, i);\n            while (seen.insert(j).second)\n                ++cand, j = A[j];\n            best = max(best, cand);\n        }\n        return best;\n    }\n};\n```
[]