No single rule
Universities handle repeats very differently. Some average every attempt (Florida), some count only the latest (Purdue, reportedly), some forgive up to a unit budget (UCLA 16 units, Berkeley 12).
Texas A&M takes another approach: a repeat after earning a B or better earns no grade points at all.
Budget-based forgiveness
Forgiveness policies usually have limits — a number of units or courses, and often an eligibility rule on the original grade (e.g. only C- or lower may be replaced).
Once the budget is used up, further attempts are averaged back into the GPA.
Check before you plan
Because the difference between 'keep all' and 'replace last' can swing a GPA by half a point, always compute under your school's rule — our university pages apply them automatically.