Recursive implementation using hash grouping