JavaScript Union Find with Rank
Union Find Intuition: https://www.quora.com/What-is-an-intuitive-explanation-of-union-find\n\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar long
Union Find Intuition: https://www.quora.com/What-is-an-intuitive-explanation-of-union-find\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar longestConsecutive = function(nums) {\n    if (nums.length===0) return 0;\n    let parent = {};\n    let size = {};\n\n    const UnionFind = function(nums){\n        for (let num of nums){\n            parent[num] = num;\n            size[num] = 1;\n        }\n\n        this.find = (n) => {\n            if (parent[n] === n) return n;\n            parent[n] = this.find(parent[n]);\n            return parent[n];\n        }\n\n        this.union = (m,n) => {\n            const rootM = this.find(m);\n            const rootN = this.find(n);\n\n            // rank\n            if (rootM === rootN) return;\n            if (size[rootM] < size[rootN]){\n                size[rootN] += size[rootM];\n                parent[rootM] = rootN;\n            }else{\n                size[rootM] += size[rootN];\n                parent[rootN] = rootM;\n            }\n        }\n    }\n\n    const uf = new UnionFind(nums);\n\n    for (let num of nums){\n        if (num-1 in parent){\n            uf.union(num-1, num);\n        }\n    }\n\n    let max = 0;\n    for (let num of nums){\n        max = Math.max(max, size[num]);\n    }\n\n    return max;\n};\n```
['Union Find', 'JavaScript']