UnionFind
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    public int numberOfComponents(int[][] properties, int k) {
        UnionFind uf = new UnionFind(properties.length);
        for (int i = 0; i < properties.length; i++) {
            Set<Integer> si = new HashSet<>();
            for (int l : properties[i]) si.add(l);
            for (int j = i + 1; j < properties.length; j++) {
                Set<Integer> s = new HashSet<>();
                for (int l : properties[j]) s.add(l);
                int jsize = s.size();
                s.addAll(si);
                if (si.size() + jsize - k >= s.size()) uf.union(i, j);
            }
        }
        return uf.setsCount;
    }

    static class UnionFind {
        int n;
        int[] roots;
        int setsCount;

        UnionFind(int n) {
            this.n = n;
            roots = new int[n];
            for (int i = 0; i < n; i++) roots[i] = i;
            setsCount = n;
        }

        boolean union(int a, int b) {
            int rootA = find(a);
            int rootB = find(b);
            if (rootA == rootB) return false;
            roots[b] = roots[rootB] = rootA;
            setsCount--;
            return true;
        }

        int find(int i) {
            if (roots[i] == i) return i;
            return roots[i] = find(roots[i]);
        }
    }
}

```
['Java']