Simple JavaScript O(n) solution
\nMethod 1 Hash set\n\nTime O(n)\nSpace O(n)\n\n\nfunction longestConsecutive(nums) {\n  if (nums == null || nums.length === 0) return 0;\n  \n  const set = new
\n**Method 1 Hash set**\n\nTime O(n)\nSpace O(n)\n\n```\nfunction longestConsecutive(nums) {\n  if (nums == null || nums.length === 0) return 0;\n  \n  const set = new Set(nums);\n  let max = 0;\n\n  for (let num of set) {\n    if (set.has(num - 1)) continue;  // make sure starting from the beginning of sequence\n\n    let currNum = num;\n    let currMax = 1;\n\n    while (set.has(currNum + 1)) {\n      currNum++;\n      currMax++;\n    }\n    max = Math.max(max, currMax);\n  }\n\n  return max;\n}\n```\n\n**Method 2**\n\nReference: https://leetcode.com/problems/longest-consecutive-sequence/discuss/41055/My-really-simple-Java-O(n)-solution-Accepted\n\nIdea: Only store the sequence length to the boundary points of the sequence\n\n```js\nfunction longestConsecutive(nums) {\n  let max = 0;\n  const lens = {};\n  \n  for (let n of nums) {\n    if (lens[n] != null) continue;\n\n    const l = lens[n - 1] || 0;   // left length\n    const r = lens[n + 1] || 0;   // right length\n\n    const len = l + r + 1;\n\n    // extend the length to the boundaries\n    lens[n - l] = len;\n    lens[n] = len;\n    lens[n + r] = len;\n\n    max = Math.max(max, len);\n  }\n\n  return max;\n}\n```\n\n
['JavaScript']