Actually what you are using is an equivalence relation and you are generating the equivalence classes of your set, "your" associativity is transitivity. Whenever you are comparing something it's almost always better to think of it as a relation instead of an algebraic structure. So you already know beforehand that your algorithm should be reflexive, symmetric and transitive (in theory, of course with computers there is numerics involved ;)
The steps to the solution are something like:
- I need all distinct elements of X
- Oh, that's a quotient set (the partition) of X by a/the equivalence relation (`==`)
- so my algorithm must be reflexive (yeah, trivially), symmetric (not so helpful) and transitive - now this I can use (together with the symmetry)
It's generally easier if you know beforehand that it must be e.g. transitive.