Does writing code for an assignment after seeing similar code online count as plagiarism?
I think this is very subjective question and will depend on who the judge is. I would suggest you communicate exactly what you wrote in the question to your professor and let him be the judge. If he thinks yes, he might assign you some other relevant exercise to evaluate your learning.

By actually being honest about your situation, you will likely be treated in a positive light.