How to verify that code you write is not copyrighted anywhere?
Don't worry about "similar" code. Copyright is about copying, not about similarity. If you wrote it independently of any other, then there shouldn't be an issue. 

But there is also the rule that in some situations there is "only one way" to say something. So if some phrases (or coding patterns) appear in several places it isn't evidence of infringement. The number of ways you can write an iteration through a list is pretty small, after all. Problems recur in computing. So the "natural" programs that solve them also recur. 

Also, copyright is about publishing. If you don't publish your code then it won't be an issue either.