Algo for grouping set operations