title: Coalesce intersecting sets into disjoint sets
body: <p>I am looking for an algorithm to coalesce a list of sets, that may intersect, into a list of sets with no intersection.</p>
  <p>For instance:</p>
  <pre><code>my_sets = set(1, 2, 3), set(5, 6), set(4, 5, 6), set(4, 7), set(3, 8), set(9)
  </code></pre>
  <p>Should yield:</p>
  <pre><code>my_coalesced_sets = set(1, 2, 3, 8), set(4, 5, 6, 7), set(9)
  </code></pre>
  <p>Ideally an algorithm O(n)...</p>
  <p>At the request of Ruben, here is one of the many algorithms I tried that does not yield correct results:</p>
  <pre><code>fun main(){
      val l = mutableListOf(setOf(1, 2, 3), setOf(5, 6), setOf(4, 5, 6), setOf(4, 7), setOf(3, 8), setOf(9))
      while (true){
          val removes = mutableListOf&lt;Set&lt;Int&gt;&gt;()
          var current = l.removeFirst()
          l.filter { current.intersect(it).isNotEmpty() }.forEach {
              current = current union it
              removes += it
          }
          l += current
          if (removes.isEmpty()){
              break
          }
          l.removeAll(removes)
      }
      print(l)
  }
  </code></pre>