Duplicate app icons in the Dock
I don't have a specific answer as to what causes macOS to do this, however there is an issue on the VS Code GitHub repository which has lots of discussions about this (for VS Code app at least).
It seems to have something to do with launching an app directly (executing the binary inside XYZ.app/Contents/MacOS) instead of via Launch Services (the open command).