# Grouping numbers based on occurrences?

Given the following three sequences of numbers, I would like to figure out how to group the numbers to find the closest relations between them.

``````1,2,3,4
4,3,5
2,1,3
...
``````

I’m not sure what the algorithm(s) I’m looking for are called, but we can see stronger relations with some of the numbers than with others.

These numbers appear together twice:

``````1 & 2
1 & 3
2 & 3
3 & 4
``````

Together once:

``````1 & 4
2 & 4
3 & 5
4 & 5
``````

So for example, we can see there must be a relationship between `1, 2, & 3` since they all appear together at least twice. You could also say that `3 & 4` are closely related since they also appear twice. However, the algorithm might pick `[1,2,3]` (over `[3,4]` ) since it’s a bigger grouping (more inclusive).

We can form any of the following groupings if we stick the numbers used most often together in a group:

``````[1,2,3] & [4,5]
[1,2]   & [3,4]   & [5]
[1,2]   & [3,4,5]
[1,2]   & [3,4]   & [5]
``````

If duplicates are allowed, you could even end up with the following groups:

``````[1,2,3,4] [1,2,3] [3,4] [5]
``````

I can’t say which grouping is most “correct”, but all four of these combos all find different ways of semi-correctly grouping the numbers. I’m not looking for a specific grouping - just a general cluster algorithm that works fairly well and is easy to understand.

I’m sure there are many other ways to use the occurrence count to group them as well. What would be a good base grouping algorithm for these? Samples in Go, Javascript, or PHP are preferred.