[C++] simple|clean code|dsu|union find
\n\n# Approach\nConsider all indexes of the array as nodes.Also note that we need to consider a value only once but as same values can be at different indices w
\n\n# Approach\nConsider all indexes of the array as nodes.Also note that we need to consider a value only once but as same values can be at different indices we need  if(mp.find(nums[i])!=mp.end()) continue;  to avoid repeting values.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n# Code\n```\nclass DisjointSet {\n\npublic:\n    vector<int> parent, size;\n    DisjointSet(int n) {\n        parent.resize(n + 1);\n        size.resize(n + 1);\n        for (int i = 0; i <= n; i++) {\n            parent[i] = i;\n            size[i] = 1;\n        }\n    }\n\n    int findUPar(int node) {\n        if (node == parent[node])\n            return node;\n        return parent[node] = findUPar(parent[node]);\n    }\n\n    void unionBySize(int u, int v) {\n        int ulp_u = findUPar(u);\n        int ulp_v = findUPar(v);\n        if (ulp_u == ulp_v) return;\n        if (size[ulp_u] < size[ulp_v]) {\n            parent[ulp_u] = ulp_v;\n            size[ulp_v] += size[ulp_u];\n        }\n        else {\n            parent[ulp_v] = ulp_u;\n            size[ulp_u] += size[ulp_v];\n        }\n    }\n};\n\n\n\n\n\nclass Solution {\npublic:\n    int longestConsecutive(vector<int>& nums) {\n        int n=nums.size();\n        if(n<=1) return n;\n        DisjointSet ds(n);\n\n        unordered_map<int,int> mp;\n\n        for(int i=0;i<n;i++){\n           \n           if(mp.find(nums[i])!=mp.end()) continue;\n             \n            mp[nums[i]]=i;\n\n            if(mp.find(nums[i]+1)!=mp.end()){\n                ds.unionBySize(i, mp[nums[i]+1]);\n            }\n            if(mp.find(nums[i]-1)!=mp.end()){\n                ds.unionBySize(i, mp[nums[i]-1]);\n            }\n        }\n\n        int max_size=INT_MIN;\n        for(int i=0;i<n;i++){\n            if(i==ds.findUPar(i)){\n                max_size=max(max_size,ds.size[i]);\n            }\n        }\n\n      return max_size;\n\n        }\n};\n```
['Union Find', 'C++']