Swift💯UnionFind
Union Find (accepted answer)\n\nclass Solution {\n    func findCircleNum(_ isConnected: [[Int]]) -> Int {\n        var groups = Array(isConnected.indices)\n    
**Union Find (accepted answer)**\n```\nclass Solution {\n    func findCircleNum(_ isConnected: [[Int]]) -> Int {\n        var groups = Array(isConnected.indices)\n        \n        func find(_ x:Int) -> Int { groups[x]==x ? x : find(groups[x]) }\n        \n        for y in isConnected.indices {\n            for x in y+1 ..< isConnected.count where isConnected[y][x] == 1 {\n                groups[find(x)] = find(y)  // form union between `x` and `y`\n            }\n        }\n\n        return Set(groups.map(find)).count\n    }\n}\n```
['Swift']