O(N) || In depth solution||
Intuition\n Describe your first thoughts on how to solve this problem. \n# Hey ppl this is the second solution with a linear complexity be sureto check the othe
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n# Hey ppl this is the second solution with a linear complexity be sureto check the other one  as well and PLS UPVOTE.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n**Steps:**\n\n1. **Handling Edge Cases:**\n   - `if(nums.length==1||nums.length==0)`: Checks for empty (`nums.length == 0`) or single-element (`nums.length == 1`) arrays. In these cases, the length itself is the longest consecutive sequence, so the function returns `nums.length`.\n\n2. **Initializing Data Structures:**\n   - `HashSet<Integer> hm = new HashSet<>()`: Creates a `HashSet` named `hm` to store the unique elements from the `nums` array. A HashSet offers fast lookup for checking element existence.\n   - `int count = 1`: Initializes a variable `count` to 1 to track the length of the current consecutive sequence encountered.\n   - `int ans = 1`: Initializes a variable `ans` to 1 to store the length of the longest consecutive sequence found so far.\n\n3. **Adding Elements to HashSet:**\n   - The `for` loop iterates through each element (`nums[i]`) in the `nums` array.\n     - `hm.add(nums[i])`: Adds the current element to the `HashSet` `hm`. This ensures efficient checking for the existence of consecutive elements later.\n\n4. **Iterating Through HashSet and Checking Sequences:**\n   - The outer `for` loop iterates through each element (`it`) in the `HashSet` `hm`.\n     - **Skipping Existing Sequences:**\n       - `if (hm.contains(it - 1)) { continue; }`: This condition checks if the previous element (one less than `it`) exists in the `HashSet`. If it does, it signifies that `it` is part of an existing sequence that has already been processed. The `continue` statement skips to the next element (`it`) in the `HashSet` to avoid redundant processing.\n     - **Finding a New Sequence Start:**\n       - `int nextVal = it + 1`: If the previous element wasn\'t found, it potentially marks the beginning of a new sequence. The variable `nextVal` stores the value expected for the next element in the sequence.\n       - `if (hm.contains(nextVal)) { ... }`: This condition checks if the `nextVal` exists in the `HashSet`. If it does, it confirms the start of a consecutive sequence.\n\n5. **Calculating Sequence Length:**\n   - **Inner Loop and Count Update:**\n     - The inner `while` loop iterates as long as elements with increasing values (`nextVal`) exist in the `HashSet`.\n       - `count++`: Increments the `count` variable for each element encountered in the sequence.\n       - `nextVal++`: Increments `nextVal` to check for the next element in the potential sequence.\n   - **Updating Longest Sequence:**\n     - `ans = Math.max(ans, count)`: After the inner loop, the current `count` represents the length of the sequence starting from `it`. The `Math.max` function compares it with the previously found longest sequence (`ans`) and updates `ans` if the current sequence is longer.\n\n6. **Resetting Count for Non-Sequences:**\n   - `else { count = 1; }`: If `nextVal` (the expected element for the sequence) wasn\'t found in the `HashSet`, it signifies that `it` is not part of a consecutive sequence. The `count` is reset to 1 for the next iteration in the outer loop.\n\n7. **Returning the Result:**\n   - After iterating through all elements in the `HashSet`, the `ans` variable holds the length of the longest consecutive sequence found. The function returns `ans`.\n\n**Key Points:**\n\n- The code uses a `HashSet` for efficient lookups to check for the existence of consecutive elements.\n- It iterates through the `HashSet` to avoid missing elements not added in the original order.\n- The inner loop and `count` variable track the length of the current consecutive sequence.\n\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- The time complexity is O(n) in the average and best cases, where n is the number of elements in the array. This is because iterating through the `HashSet` (which has an average lookup time of O(1)) dominates the processing time.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n# Code\n```\nclass Solution {\n    public int longestConsecutive(int[] nums) {\n        if(nums.length==1||nums.length==0){\n            return nums.length;\n        }\n        HashSet<Integer> hm=new HashSet<>();\n        int count=1;\n        int ans=1;\n        for(int i=0;i<nums.length;i++){\n            hm.add(nums[i]);\n        }\n        for (Integer it : hm) {\n  if (hm.contains(it - 1)) {\n    continue;\n  }\n  int nextVal = it + 1;\n  if (hm.contains(nextVal)) {\n    count = 1; // Reset count for a new sequence\n    while (hm.contains(nextVal)) {\n      count++;\n      nextVal++;\n    }\n    ans = Math.max(ans, count);\n  } else {\n    count = 1; // Since nextVal wasn\'t there, this element is not part of a sequence. Reset count to 1.\n  }\n}\n\n        return ans;\n    }\n}\n```
['Hash Table', 'Java']