In the Han Dynasty, there was an official named Yang Yun, who was both capable and honest. Commenting on the assassination of a king of a small state, he said, "If a king refuses to follow the advice of a wise minister, he will suffer an untimely death. The emperor of the Qin Dynasty trusted treacherous ministers, and therefore lost his state." He compared kings and emperors to raccoons living on the same mountain.
This idiom is used derogatorily to refer to people who are of the same (bad) kind.