Did Rupert know that it was David's hat?
We can't tell as the camera zooms in on the initials inside the hat. When the camera zooms out, it appears that Rupert had been looking down, but not far enough down to see the initials, which were facing away from him. However, after that he seems to act as if he's seen a ghost.
Another thought is that almost everyone had left by the time he was leaving. Did he figure it out based on the fact that there was an extra hat in the closet?