We can try
library(zoo)
a[duplicated(a)] <- NA
a[!is.na(a)] <- seq_along(a[!is.na(a)])
na.locf(a)
#[1] 1 2 3 4 4 5 5 5
Or another option is
cumsum(ave(a, a, FUN=seq_along)==1)
#[1] 1 2 3 4 4 5 5 5
Or a compact option would be
library(splitstackshape)
getanID(a)[, cumsum(.id==1)]
#[1] 1 2 3 4 4 5 5 5