The chief executive of US confectionery giant Hershey is to leave the company at the end of the year amid speculation that he had grown frustrated with its ownership structure.