My Java Solution using UnionFound
    \n    public class Solution {\n            public int longestConsecutive(int[] nums) {\n                UF uf = new UF(nums.length);\n                Map<In
        \n    public class Solution {\n            public int longestConsecutive(int[] nums) {\n                UF uf = new UF(nums.length);\n                Map<Integer,Integer> map = new HashMap<Integer,Integer>(); // <value,index>\n                for(int i=0; i<nums.length; i++){\n                    if(map.containsKey(nums[i])){\n                        continue;\n                    }\n                    map.put(nums[i],i);\n                    if(map.containsKey(nums[i]+1)){\n                        uf.union(i,map.get(nums[i]+1));\n                    }\n                    if(map.containsKey(nums[i]-1)){\n                        uf.union(i,map.get(nums[i]-1));\n                    }\n                }\n                return uf.maxUnion();\n            }\n        }\n        \n        class UF{\n            private int[] list;\n            public UF(int n){\n                list = new int[n];\n                for(int i=0; i<n; i++){\n                    list[i] = i;\n                }\n            }\n            \n            private int root(int i){\n                while(i!=list[i]){\n                    list[i] = list[list[i]];\n                    i = list[i];\n                }\n                return i;\n            }\n            \n            public boolean connected(int i, int j){\n                return root(i) == root(j);\n            }\n            \n            public void union(int p, int q){\n              int i = root(p);\n              int j = root(q);\n              list[i] = j;\n            }\n            \n            // returns the maxium size of union\n            public int maxUnion(){ // O(n)\n                int[] count = new int[list.length];\n                int max = 0;\n                for(int i=0; i<list.length; i++){\n                    count[root(i)] ++;\n                    max = Math.max(max, count[root(i)]);\n                }\n                return max;\n            }\n        }
['Java']