I'm not sure what the best way to do draw this on SO, but basically, you want to compare adjacent cells and increment if same and reset to 1 if not, right?
A1: "Apple" B1: 1 A2: "Apple" B2: =if(A2=A1, B1+1, 1) A3: "Apple" B3: =if(A3=A2, B2+1, 1) (and so on)