When mine hits, she's usually tired and it's bedtime or nap time. That's just how she works. She gets a little aggressive. When she has done it at other times, I had read to use their hand and show them how to be gentle. Tell them that hurts, touch like this instead, and be gentle. Literally use their hand and put their hand on whomever they were hitting and model a gentle touch. They need that biofeedback of the sensation of touching gently. Best wishes!
My daughter does the same thing 😭