JavaScript Clean Union Find
javascript\nvar findCircleNum = function(M) {\n  \n    class UnionFind {\n        constructor(n) {\n            this.graph = [...Array(n)].map((_, i) => i);\n  
```javascript\nvar findCircleNum = function(M) {\n  \n    class UnionFind {\n        constructor(n) {\n            this.graph = [...Array(n)].map((_, i) => i);\n            this.groups = n;\n        }\n        \n        find(id) {\n            if(this.graph[id] === id) return id;\n            this.graph[id] = this.find(this.graph[id]);\n            return this.graph[id];\n        }\n        \n        union(x, y) {\n            const rootX = this.find(x);\n            const rootY = this.find(y);\n            if(rootX !== rootY) {\n                this.graph[rootY] = rootX;\n                this.groups--;\n            }\n        }\n    }\n\n    const N = M.length, unionfind = new UnionFind(N);\n    \n    for(let r = 0; r < N; r++) {\n        for(let c = 0; c < N; c++) {\n            if(M[r][c]) unionfind.union(r, c);\n        }\n    }\n    return unionfind.groups;\n};\n```
['Union Find', 'JavaScript']