Broadly speaking there are three reasons to do research:
- To push the boundaries of human knowledge forward for the betterment of humanity
- Because you personally find it interesting and exciting and rewarding
- Because the knowledge gives an organisation a competitive advantage over another organisation.
(Note these are not mutually exclusive)
If you are in the first camp, then other people working on your problems is good. If someone figures out what you've spent a year pondering without progress then great! That problem is now solved and you can move on to the next step.
If you are in the second camp and you feel that someone else figuring out the answer deprives you of the satisfaction you would get from making the discovery yourself, then just don't publish - since you are only doing for your own benefit why bother with publishing?
In the third situation you also wouldn't publish, and doing so might even be breaking your contract or even the law. However, you would have to give your work to your employer, if you had one, and I guess you might be scooped by someone else in the org. But that's their right, as you are being paid to better the org, not better yourself.
You tagged your post "independent-researcher" this is usually how people are described if their research is not what they do for a living. In which case, if you want to keep your research to yourself, that's entirely your right. If you are employed to do research in what we might broadly call a public institution (like a university), then you may be forced to publish to keep your reputation up and therefore keep your job. But again, this is right. Someone (generally, ultimately the public) is paying your salary, and they deserve the knowledge in return. And the quicker the better, since as the funder, it is their knowledge, and things advancing faster is in their interests.