Simple C++ solution|| Using Two different Approaches
If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries 
**If you really found my solution helpful please upvote it, as it motivates me to post such kind of codes and help the coding community, if you have some queries or some improvements please feel free to comment and share your views.**\n\n```\n//1st Approach\n    int longestConsecutive(vector<int>& nums) {\n        if(nums.size()==0)\n            return 0;\n        sort(nums.begin(),nums.end());\n        int len=1;\n        int mx=1;\n        for(int i=1;i<nums.size();i++)\n        {\n            if(nums[i-1]+1==nums[i])\n            {\n                len++;\n                mx=max(len,mx);\n            }\n            else if(nums[i-1]==nums[i])\n                continue;\n            else\n                len=1;\n        }\n        return mx;\n    }\n\n//TC: O(nlogn)\n//SC: O(1)\n\n//2nd Approach\n int longestConsecutive(vector<int>& nums) {\n        if(nums.size()==0)\n            return 0;\n       unordered_set<int> s;\n        for(int i=0;i<nums.size();i++)\n            s.insert(nums[i]);\n        int mx=1;\n        for(int i=0;i<nums.size();i++)\n        {\n            if(s.find(nums[i]-1)==s.end())\n            {\n                int len=1;\n                int curr=nums[i];\n                while(s.find(curr+1)!=s.end())\n                {\n                    curr=curr+1;\n                    len++;\n                }\n                mx=max(mx,len);\n            }\n        }\n        return mx;\n    }\n\n//TC: O(3n)\n//SC: O(n)\n```
['C', 'C++']