Two Approaches || Unordered Map and Sorting with Updating || Java and Cpp Solution
Time Complexity: O(N log(N))\n\nJava Code\n\n\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        int size = nums.length;\n        \n   
**```Time Complexity: O(N log(N))```**\n\n**```Java Code```**\n\n```\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        int size = nums.length;\n        \n        if(size == 0) return 0;\n        \n        Arrays.sort(nums); // sorting the array in O(N log(N))\n        \n        int longest = 0;\n        int current_longest = 1;\n        \n        for(int i=1;i<size;i++){\n            \n            if(nums[i] == nums[i-1]){\n                continue; //same element as previous skip this\n            }\n            \n            else if(nums[i] == nums[i-1] + 1){\n                current_longest++; //current element is consecutive so increase it.\n            }\n            \n            else{\n                longest = Math.max(longest,current_longest);\n                current_longest = 1; // we can\'t extend so update longest as the maximum of the both and reset the current_longest.\n            }\n            \n        }\n        \n        return Math.max(longest,current_longest);\n    }\n}\n```\n\n**```C++  Code```**\n\n```\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        int size = nums.size();\n        if(!size) return 0;\n        sort(nums.begin(), nums.end());\n        int longest = 0;\n        int current_longest = 1;\n        for(int i=1;i<size;i++){\n            if(nums[i] == nums[i-1]) continue; //same element as previous skip this\n            else if(nums[i] == nums[i-1] + 1) current_longest++; //current element is consecutive so increase it.\n            else longest = max(longest,current_longest), current_longest = 1; // we can\'t extend so update longest as the maximum of the both and reset the current_longest.\n        }\n        return max(longest,current_longest);\n    }\n};\n```\n\n<hr>\n\n**```We can write an algorithm in O(N) complexity```**\n\n```\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        unordered_set<int> s(begin(nums),end(nums));\n        int longest = 0;\n        for(auto& i:s){\n            if(s.count(i-1)) continue;\n            int j=1;\n            while(s.count(i+j)) j++;\n            longest = max(longest,j);\n        }\n        return longest;\n    }\n};\n```\n\n<hr>\n\nThe first two codes are in complexity O(N log(N)) and the second one is in O(N)\n\nsuppose the array length is 4 then \nO(N log(N)) = O(4 log(4)) = O(2.40823) => Time is upto 2.40823{parameter of time}\nO(N) = O(4) => Time is upto 4{parameter of time}\n\nFrom this we conclude that  **```O(N log(N)) is better than O(N)```**.  **NOPE IT\'S WRONG**\n\n**```Reason```**: \nThe computer has the log of base 2 not the base of 10. \nO(N log(N)) = O(4 log(4)) = O(4 * 2 * log2{base 2}) = O(8) = 8{parameters of time}\nO(N) = O(4) = 4{parameters of time}\n\nSo from here we can conclude : **```O(N) < O(N log(N))```**.\n\nAlways remember now: **```O(N) is always better than O(N log(N))```**\n\n**PS**: **_```Upvote if it helps you```_**
['Sorting', 'Ordered Set', 'C++', 'Java']