Very simple, easy to understand Kotlin solution.
Idea is to add all numbers to a set, so that when you loop through nums you can check whether or not the neighbors are in the set. To avoid counting the same ch
Idea is to add all numbers to a set, so that when you loop through nums you can check whether or not the neighbors are in the set. To avoid counting the same chain of consecutive numbers more than once, remove those neighbor numbers from the hash set. This ensures that you only ever access each element once in th second loop.\n\ncomplexity requires two for loops. One or the hash set, and the other to count the consecutive chains. O(2n) = O(n)\n\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(n) needed for hash set\n# Code\n```\nclass Solution {\n    fun longestConsecutive(nums: IntArray): Int {\n        val map = nums.toHashSet()\n\n        var max = 0\n        for (num in nums) {\n            if (!map.contains(num)) {\n                continue\n            }\n\n            var left = num\n            var right = num\n\n            while (map.contains(--left)) {\n                map.remove(left)\n            }\n            while(map.contains(++right)) {\n                map.remove(right)\n            }\n            \n            var count = right-left-1;\n            if (count > max) {\n                max = count\n            }\n        }\n        return max\n    }\n}\n```
['Kotlin']