Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.

You must write an algorithm that runs in&nbsp;O(n)&nbsp;time.

&nbsp;
Example 1:

Input: nums = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.


Example 2:

Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9


&nbsp;
Constraints:


	0 &lt;= nums.length &lt;= 105
	-109 &lt;= nums[i] &lt;= 109


class Solution(object):
    def longestConsecutive(self, nums):
        ans=0
        nums=set(nums)
        count=0
        for i in nums:
            if i-1 not in nums:
                j=i
                count=0
                while j in nums:
                    count+=1
                    j+=1
                ans=max(ans,count)            
        return ans
class Solution {
    public int longestConsecutive(int[] nums) {
        Set<Integer> storage = new HashSet();
        
        for(int i = 0; i < nums.length; i++){
            storage.add(nums[i]);
        }
        
        int maxL = 0;
        
        for(int i = 0; i < nums.length; i++){
            
            //check if nums[i] id present in set or not
            //since after checking in set we remove the element from 
            //set, there is no double calculation for same sequence 
            if(storage.contains(nums[i])){
                storage.remove(nums[i]);
                
                int dec = nums[i]-1;
                int inc = nums[i]+1;
                int tempL = 1;
                
                //check both ways from nums[i] and calculate 
                //tempL. since we are removing elements from 
                //set we only calculate once for every sequence.
                
                while(storage.contains(dec)){
                    storage.remove(dec);
                    dec--;
                    tempL++;
                }
                
                while(storage.contains(inc)){
                    storage.remove(inc);
                    inc++;
                    tempL++;
                }
                
                
                maxL = Math.max(maxL, tempL);
                
            }
        }
        
        return maxL;
    }
}
class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        
        set<int> hashSet;
        
        int longestConsecutiveSequence = 0; 
        
        for(auto it : nums){
            hashSet.insert(it);
        }
        
        for(auto it : nums){
            
            if(!hashSet.count(it-1)){
                
                int count = 1;
                
                while(hashSet.count(it+count)) ++count;
                
                longestConsecutiveSequence = max(count,longestConsecutiveSequence);
                
            }
            
        }
        
        return longestConsecutiveSequence;
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function(nums) {
  if(nums.length === 1){
    return 1
  }
  if(nums.length === 0){
    return 0
  }
  nums.sort((a, b) => a - b)
  let result = 1
  let streak = 1
  let i = 0
  while(i < nums.length - 1){
    if(nums[i] == nums[i + 1]) {
      i++ 
      continue
    }
    if(nums[i] == nums[i + 1] - 1){
      streak++
       if(streak > result){
         result = streak
       }
    } else {
      streak = 1
    }
    i++
  }
  return result
};
Longest Consecutive Sequence