C++/Java/Python/JavaScript || ✅🚀Simple Code || ✔️🔥Easy To Understand
Intuition:\nThe problem requires finding the length of the longest consecutive sequence of numbers in the given array. We can sort the array and then traverse i
# Intuition:\nThe problem requires finding the length of the longest consecutive sequence of numbers in the given array. We can sort the array and then traverse it to find the longest consecutive sequence.\n\n# Approach:\n1. Sort the given array.\n2. Initialize two variables: count and temp. count will store the length of the longest consecutive sequence found so far, and temp will store the length of the current consecutive sequence.\n3. Traverse the sorted array from the first element to the second last element:\n   - If the current element is one less than the next element, increment temp.\n   - If the current element is equal to the next element, do nothing.\n   - If the current element is not one less than the next element, reset temp to 1.\n   - After each iteration, update count with the maximum value of count and temp.\n4. Return count.\n\n# Complexity\n- Time complexity: Sorting the array takes O(n log n) time. The for loop takes O(n) time. Therefore, the overall time complexity of the function is O(n log n + n) which simplifies to O(n log n).\n\n- Space complexity: We are not using any additional data structure that grows with the size of the input. Therefore, the space complexity of the function is O(1).\n\n---\n# C++\n```\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        int count=0;\n        int temp=1;\n        if(nums.size()==0){\n            return 0;\n        }\n        if(nums.size()==1){\n            return 1;\n        }\n        for(int i=0;i<nums.size()-1;i++){\n            if(nums[i]==(nums[i+1]-1)){\n                temp++;\n                count = max(count,temp);\n            }\n            else if(nums[i]==(nums[i+1])){\n                count = max(count,temp);\n            }\n            else{\n                temp=1;\n                count = max(count,temp);\n            }\n        }\n        return count;\n    }\n};\n```\n---\n# Java\n```\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        Arrays.sort(nums);\n        int count = 0;\n        int temp = 1;\n        if (nums.length == 0) {\n            return 0;\n        }\n        if (nums.length == 1) {\n            return 1;\n        }\n        for (int i = 0; i < nums.length - 1; i++) {\n            if (nums[i] == (nums[i + 1] - 1)) {\n                temp++;\n                count = Math.max(count, temp);\n            } else if (nums[i] == (nums[i + 1])) {\n                count = Math.max(count, temp);\n            } else {\n                temp = 1;\n                count = Math.max(count, temp);\n            }\n        }\n        return count;\n    }\n}\n```\n---\n# Python\n```\nclass Solution(object):\n    def longestConsecutive(self, nums):\n        nums.sort()\n        count = 0\n        temp = 1\n        if len(nums) == 0:\n            return 0\n        if len(nums) == 1:\n            return 1\n        for i in range(len(nums) - 1):\n            if nums[i] == (nums[i + 1] - 1):\n                temp += 1\n                count = max(count, temp)\n            elif nums[i] == (nums[i + 1]):\n                count = max(count, temp)\n            else:\n                temp = 1\n                count = max(count, temp)\n        return count\n```\n---\n# JavaScript\n```\nvar longestConsecutive = function(nums) {\n    nums.sort((a, b) => a - b);\n    let count = 0;\n    let temp = 1;\n    if (nums.length === 0) {\n        return 0;\n    }\n    if (nums.length === 1) {\n        return 1;\n    }\n    for (let i = 0; i < nums.length - 1; i++) {\n        if (nums[i] === nums[i + 1] - 1) {\n            temp++;\n            count = Math.max(count, temp);\n        } else if (nums[i] === nums[i + 1]) {\n            count = Math.max(count, temp);\n        } else {\n            temp = 1;\n            count = Math.max(count, temp);\n        }\n    }\n    return count;\n};\n```
['Python', 'C++', 'Java', 'JavaScript']