fork download
  1. object Main extends App {
  2.  
  3. val l = List("A", "A", "C", "C", "B", "C")
  4.  
  5. def appendCount(li: List[String]): List[String] = {
  6. val counts = l.foldLeft(Map[String, Int]())((acc, e) => acc + (e -> (acc.getOrElse(e, -1) + 1)))
  7. val (appendedList, _) = l.foldRight(List[String](), counts){ case (e, (li, m)) =>
  8. (s"$e${m(e)}" :: li, m + (e -> (m(e) - 1)))
  9. }
  10. appendedList
  11. }
  12.  
  13. println(appendCount(l))
  14. }
Success #stdin #stdout 0.4s 4382720KB
stdin
Standard input is empty
stdout
List(A0, A1, C0, C1, B0, C2)