I have an list that I would like to reduce according to rules for combination. Examples of the input list (this will be a list of pairs and may be several hundred elements long) and the output I’m looking for with some function f are:

```
f({{1,4},{1,6}})
-> {{1,4,6}}
```

The two input lists both contain 1 and are combined. Or

```
f({{1, 4}, {1, 6}, {2, 3}})
-> {{1,4,6},{2,3}}
```

As in the first but {2,3} has no common elements with the others and remains separate.

One way of doing this is with a repeated rule:

```
{{1, 4},{1,6}, {2, 3}} //. {head___, {x_, y_}, mid___, {x_, z_} | {z_, x_} | {y_, z_} | {z_, y_}, tail___} -> {head, {x, y, z}, mid, tail}
-> {{1,4,6},{2,3}}
```

This produces the correct output but has a problem that some other inputs makes clear – the last {4,6} term should have been combined with {1,4,6} and disappear:

```
{{1, 4},{1, 6}, {2, 3}, {4, 6}}//. {head___, {x_, y_}, mid___, {x_, z_} | {z_, x_} | {y_, z_} | {z_, y_}, tail___} -> {head, {x, y, z}, mid, tail}
-> {{1, 4, 6}, {2, 3}, {4, 6}}
```

The correct output should be:

```
{{1,4,6},{2,3}}
```

Or

```
{{1, 4}, {1, 6}, {2, 3}, {4, 6}, {3, 5}}//. {head___, {x_, y_}, mid___, {x_, z_} | {z_, x_} | {y_, z_} | {z_, y_}, tail___} -> {head, {x, y, z}, mid, tail}
-> {{1, 4, 6}, {2, 3, 5}, {4, 6}}
```

The correct output should be:

```
{{1, 4, 6}, {2, 3, 5}}
```

The repeated rule above builds longer lists than pairs (there are then not matched in subsequent iterations) but I haven’t found a way of writing a pattern to deal with it (so maybe rule replacement isn’t the way to go here), I’ve tried a couple of other approaches of a more functional nature but seem to be missing something.

Any suggestions?