Anyone who makes smartass remarks to this post...you guys are going to Hell. So bugger off. :-P
I had it when I was little. I would count my steps, how many bites of food I took, re-shut doors "just right" to keep people from dying, and wash my hands compulsively. It sucked, but it went away by itself when I was about 10. (I was afraid to tell my parents about it because they already got angry with me for washing my hands so much) So mostly, I just tried to ignore it and it gradually stopped. But even now I'll get days where I have to make up a phrase in my head and repeat it over and over to keep myself from counting random stuff.
Yeah, I can relate to that. I have a tendency to do really weird shit like making really long and pointless lists trying to remember every movie I've ever seen, and such... I'm a very goal-oriented person by nature, anyway. so it makes it a bit difficult to discern when I'm just being focused and when I'm behaving irrationally. I usually just use a rule of thumb when I realize I'm hyperfocusing and I take a step back, and decide whether the end result of said action is actually worth the time I'm spending on it. If it's not, then it's probably something compulsive and I try to drag myself away from it.