This is puzzling. Restart the Mac in Safe Mode (Start up your Mac in safe mode - Apple Support). This will clean up the computer and fix potential issues. Note that Safe Mode can take a while to complete, i.e. more than 10 min depending on the computer.
Once in Safe Mode, restore the contacts following the steps in my previous post. Add a few more contacts for testing purposes. Then restart the iMac in normal mode. Then check that the test contacts are there on the iMac. Then go to iCloud.com and check that those contacts are also there.
Note that Time Machine is not going to help for missing contacts if the contacts are synch in iCloud. It would only work if they were local to the iMac.
If the issue recurs please contact Apple Support as I'm not sure what the root of the problem is: Get Support