✅C++ SOLUTION ☑️
\n# PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_
\n# **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/_pratay_nandy_/]()**\n\n# Approach\n\n\n### Let\'s break it down:\n\n1. **`unordered_set<int> s;`**: Initializes an unordered set `s` to store unique integers.\n\n2. **`for(int num : nums){ s.insert(num); }`**: Iterates through each element in the `nums` vector and inserts it into the set `s`. This ensures that the set contains unique elements from the input vector.\n\n3. **`int ans = 0;`**: Initializes a variable `ans` to store the final result, which represents the length of the longest consecutive sequence.\n\n4. **`for(int num : nums){`**: Iterates through each element in the `nums` vector.\n\n5. **`if(s.find(num-1) == s.end()){`**: Checks if the previous element (`num-1`) is not present in the set. If true, it means the current element `num` is the start of a potential consecutive sequence.\n\n6. **`int currentElement = num; int count = 1;`**: Initializes variables to keep track of the current element and the count (length of the consecutive sequence). Since the current element is the start of a new sequence, the count is set to 1.\n\n7. **`while(s.find(currentElement+1) != s.end()){ currentElement++; count++; }`**: Uses a while loop to iterate forward in the set and count the consecutive elements. This loop continues until there is no consecutive element (`currentElement+1`) in the set.\n\n8. **`ans = max(ans, count);`**: Updates the `ans` variable with the maximum of its current value and the count obtained for the current consecutive sequence.\n\n9. **`}`**: Closes the loop for iterating through the elements in the `nums` vector.\n\n10. **`return ans;`**: Returns the final result, which represents the length of the longest consecutive sequence.\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n<!-- - Time complexity:**0()** -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:**0(N)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n![upvote2.png](https://assets.leetcode.com/users/images/6198d926-527c-42f7-a663-aaf8d62ed44c_1704980133.0159707.png)\n\n\n# Code\n```\n\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        unordered_set<int> s;\n        \n         //dump all elements into set\n        for(int num : nums){\n            s.insert(num);\n        }\n        \n        \n        int ans = 0;\n        for(int num : nums){\n            //check if previous_element is in set\n            if(s.find(num-1) == s.end()){\n                int currentElement = num;\n                int count = 1;\n                \n                //start a chain from current element\n                while(s.find(currentElement+1) != s.end()){\n                    currentElement++;\n                    count++;\n                }\n                ans = max(ans, count);\n            }\n        }\n        return ans;\n    }\n};\n```
['Array', 'C++']