This will automate the displaying of headers:
Formula: copy to clipboard
=IFERROR(IF(MATCH(0,INDEX(COUNTIF($D1:D1,$A2:$A8),,),)>COUNTA($A2:$A8),"",INDEX($A2:$A8,MATCH(0,INDEX(COUNTIF($D1:D1,$A2:$A8),,),))),"")

Select cell E1,
Paste the formula into the formula bar,
Drag the fill handle over to cell H1.
Let us know if you have any questions.