✅☑️Beats 100% || Easy Understand - Beginner Solution || O(N) Step by Step Explained
Intuition\nHashSet Construction:\nIt converts the input array into a HashSet (arr) for faster element lookup.\n\nIterating Through Elements:\nIt iterates throug
# Intuition\n**HashSet Construction:**\nIt converts the input array into a HashSet (arr) for faster element lookup.\n\n**Iterating Through Elements:**\nIt iterates through each element in the original array.\nFor each element, it checks if the element one less than the current element is not present in the set. If not, it means the current element is the potential start of a new consecutive sequence.\n\n****Consecutive Sequence Length Calculation:****\nIt initializes a counter (curr) to zero and starts incrementing it while the next consecutive element is present in the set.\nThe variable x is used to explore consecutive elements.\nThe maximum consecutive sequence length (maxx) is updated whenever a longer consecutive sequence is found.\n\n# PLEASE UPVOTE IF LIKE THE SOLUTION. THANKS\n\n# Complexity\n- Time complexity:O(2N+N) = O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# PYTHON 3\n```\nclass Solution:\n    def longestConsecutive(self, nums: List[int]) -> int:\n        arr = set(nums)\n        maxx = 0\n        \n        for i in nums:\n            x = i - 1\n            #checking current max increasing length\n            curr = 0\n            \n            # Check if the element one less than the current element is not in the set \'arr\'\n            if x not in arr:\n                # If not, it means the current element is the start of a new consecutive sequence\n                # Increment \'curr\' while the next consecutive element is in the set \'arr\'\n                while x + 1 in arr:\n                    curr += 1\n                    x += 1\n                \n                maxx = max(maxx, curr)\n        \n        return maxx          \n        \n```\n\n# JAVA\n```\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        HashSet<Integer> arr = new HashSet<>();\n        for (int num : nums) {\n            arr.add(num);\n        }\n        \n        int maxx = 0;\n        \n        for (int i : nums) {\n            int x = i - 1;\n            int curr = 0;\n            \n            if (!arr.contains(x)) {\n                while (arr.contains(x + 1)) {\n                    curr += 1;\n                    x += 1;\n                }\n                \n                maxx = Math.max(maxx, curr);\n            }\n        }\n        \n        return maxx;\n    }\n}\n```\n\n# CPP\n```\nclass Solution {\npublic:\n    int longestConsecutive(std::vector<int>& nums) {\n        unordered_set<int> arr(nums.begin(), nums.end());\n        int maxx = 0;\n        \n        for (int i : nums) {\n            int x = i - 1;\n            int curr = 0;\n            \n            if (arr.find(x) == arr.end()) {\n                while (arr.find(x + 1) != arr.end()) {\n                    curr += 1;\n                    x += 1;\n                }\n                \n                maxx = max(maxx, curr);\n            }\n        }\n        \n        return maxx;\n    }\n};\n\n```
['Array', 'Hash Table', 'Union Find', 'Python', 'C++', 'Java', 'Python3']