Using union set to reduce the memory usage