If all that connected the two was the word “Egypt” this could be dismissed as mere coincidence.
But on a deeper level the verse reminds us that the Egyptians oppressed minority groups and this is an example which should be rejected.
Rashi says the ways of Egypt were morally corrupt, not just in the way they treated minorities. He also quotes a rabbinic source that says that the Egyptians followed unacceptable social customs like frequenting theatres and gladiatorial fights.
Commentators point out that these were Roman practices, but the cruelty and lack of modesty they entailed certainly come under the heading of moral corruption.
Over and above these examples, a number of sources say that the Egyptians blurred the distinction between males and females: males acted in feminine ways and vice-versa, which warns us not to blur the boundaries or to pretend to be what you are not and cannot be.
Why is the verse so stern in its warning to the people of Israel?
Because Egypt was the first nation with whom the Israelites as a group had any dealings, and the Egyptians must not be seen as a role model worthy of emulation.