refactor the equalSets function