You're looking for the
g <- GRanges(seqnames=c(rep("Chr1",6)), strand=rep("+",6), ranges=IRanges(start=c(2,2,2,4,5,6), end=c(24,25,25,25,25,21)))
g2 <- unique(g)
g2$hits<- countOverlaps(g2, g, type="equal")
You could also run
countOverlaps(g,g,type="equal")first and then use
unique(). You'll get the same result either way.