[Question]
  [
Inspired by [this Stack Overflow post](https://stackoverflow.com/q/70550089/17242583).
Given an ascending-sorted array of possibly duplicated integers, your goal is to increment each number by a counter, starting at 0, that resets for each group.
Spec:
* Any numbers may be negative (but if so, they'll be at the beginning, because the array is sorted),
* The array will have at least one element,
* There may be any number of integers in one group
* The groups of numbers have nothing to do with one another
To demonstrate:
```
[1, 1, 1, 1, 10, 10, 20, 20, 20, 30, 40, 40, 40, 40]
```
should become this
```
[1, 2, 3, 4, 10, 11, 20, 21, 22, 30, 40, 41, 42, 43]
```
because
```
  1 1 1 1   10 10   20 20 20   30   40 40 40 40
+ 0 1 2 3    0  1    0  1  2    0    0  1  2  3
  -------   -----   --------   --   -----------
  1 2 3 4   10 11   20 21 22   30   40 41 42 43   
```
## Test cases
```
input -> output
[1, 2, 3] -> [1, 2, 3]
[1, 1, 2, 2, 3, 3] -> [1, 2, 2, 3, 3, 4]
[0, 0, 0, 0, 0] -> [0, 1, 2, 3, 4]
[1, 1, 10, 10, 100, 100, 100, 100] -> [1, 2, 10, 11, 100, 101, 102, 103]
[-5, -5, -5, -5, -4, 4, 4, 4, 4, 9, 9] -> [-5, -4, -3, -2, -4, 4, 5, 6, 7, 9, 10]
[1, 1, 1, 1, 2, 2] -> [1, 2, 3, 4, 2, 3]
```
  
[Answer]
# [BQN](https://mlochbaum.github.io/BQN/), 3 bytes[SBCS](https://github.com/mlochbaum/BQN/blob/master/commentary/sbcs.bqn)
Perfect challenge for BQN's *occurence count* builtin.
```
+⟜⊒
```
[Run online!](https://mlochbaum.github.io/BQN/try.html#code=RiDihpAgK+KfnOKKkgoKPuKLiOKfnEbCqCDin6gx4oC/MuKAvzMKMeKAvzHigL8y4oC/MuKAvzPigL8zCjDigL8w4oC/MOKAvzDigL8wCjHigL8x4oC/MTDigL8xMOKAvzEwMOKAvzEwMOKAvzEwMOKAvzEwMArCrzXigL/CrzXigL/CrzXigL/CrzXigL/CrzTigL804oC/NOKAvzTigL804oC/OeKAvzkKMeKAvzHigL8x4oC/MeKAvzLigL8y4p+p)
The modifier `⟜` composes two functions. If there is a single argument (as is the case here), the right function is called on that argument, and then the left function is called with that result and the original argument:
```
(f⟜g x) ≡ (x f g x)
```
The builtin `⊒` takes a vector and returns for each element how many times the same value appeared before:
```
(⊒ 1‿1‿1‿10‿10‿100) ≡ 0‿1‿2‿0‿1‿0
```
The result of `⊒` is then added element-wise to original input by `+`:
```
(1‿1‿1‿10‿10‿100 + 0‿1‿2‿0‿1‿0) ≡ 1‿2‿3‿10‿11‿100
```
---
Ports of other more interesting answers:
* [`⊢+⊒˜-⊐˜`](https://mlochbaum.github.io/BQN/try.html#code=RiDihpAg4oqiK+KKksucLeKKkMucCgo+4ouI4p+cRsKoIOKfqDHigL8y4oC/Mwox4oC/MeKAvzLigL8y4oC/M+KAvzMKMOKAvzDigL8w4oC/MOKAvzAKMeKAvzHigL8xMOKAvzEw4oC/MTAw4oC/MTAw4oC/MTAw4oC/MTAwCsKvNeKAv8KvNeKAv8KvNeKAv8KvNeKAv8KvNOKAvzTigL804oC/NOKAvzTigL854oC/OQox4oC/MeKAvzHigL8x4oC/MuKAvzLin6k=), [DLosc in Python](https://codegolf.stackexchange.com/a/240425)
* [`(⊢+˝=⌜×⊒<⌜⊒)˜`](https://mlochbaum.github.io/BQN/try.html#code=RiDihpAgKOKKoivLnT3ijJzDl+KKkjzijJziipIpy5wKCj7ii4jin5xGwqgg4p+oMeKAvzLigL8zCjHigL8x4oC/MuKAvzLigL8z4oC/Mwow4oC/MOKAvzDigL8w4oC/MAox4oC/MeKAvzEw4oC/MTDigL8xMDDigL8xMDDigL8xMDDigL8xMDAKwq814oC/wq814oC/wq814oC/wq814oC/wq804oC/NOKAvzTigL804oC/NOKAvznigL85CjHigL8x4oC/MeKAvzHigL8y4oC/MuKfqQ==), [Luis Mendo in MATL](https://codegolf.stackexchange.com/a/240426)
[Answer]
# [Python 3.8](https://docs.python.org/3/), ~~48~~ 46 bytes
*2-byte savings jointly contributed by [Jonathan Allan](https://codegolf.stackexchange.com/users/53748) and [dingledooper](https://codegolf.stackexchange.com/users/88546)*
```
lambda L,j=1:[n-(j:=j-1)-L.index(n)for n in L]
```
[Try it online!](https://tio.run/##XU5BDoIwELz3FRNONNkaKppok/oCfoA9YIAI0UKgB319tRAUTWY2O5PJ7vRPd@1seugHX@uzvxX3S1kgo1ZLlVsRt0q3QnKRbRpbVo/Y8robYNFYZMa7anQjNHKGXBK2hNTQvM8yOIuZEL5YxWSy8H9MIbEn/HBHWOP4xvra57VhhoWyoWToO5VVDP3QWBcHRYjEKSLUk@LcvwA "Python 3.8 (pre-release) – Try It Online")
### Explanation
The original 48-byte solution is easier to explain:
```
lambda L:[n+i-L.index(n)for i,n in enumerate(L)]
```
Consider each number \$n\$ in the list together with its index \$i\$. Suppose that we are looking at the \$k^{th}\$ occurrence (0-indexed) of the number \$n\$. We want to add \$k\$ to the number. Since identical numbers are adjacent, the index of the \$0^{th}\$ occurrence of \$n\$ will be at index \$i-k\$. This is the result of `L.index(n)`, meaning \$k\$ is `i-L.index(n)`. Add that value to \$n\$ for each element of the list, and we're done.
To get the 46-byte solution, we track the index using a variable instead of `enumerate`, updating it in the list comprehension using Python 3.8's "walrus operator" `:=`. We could track the actual index \$i\$:
```
lambda L,i=-1:[n+(i:=i+1)-L.index(n)for n in L]
```
but it saves a byte to track \$j \equiv -i\$ instead, because we can initialize it to `1` instead of `-1`.
[Answer]
# [J](http://jsoftware.com/), 9 bytes
```
+i.@#-i.~
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/tTP1HJR1M/Xq/mty@TnpKURbWds4aIBFNfX16rhSkzPyFdIUDHWQkAEEGyFhYyA2QcH/AQ "J – Try It Online")
Basically a port of [DLosc's python answer](https://codegolf.stackexchange.com/a/240425/15469), which fits naturally in the array paradigm.
## how
Consider `1, 1, 1, 1, 10, 10, 20, 20, 20, 30, 40, 40, 40, 40`
* `i.~` Index of each element's first appearance:
```
0 0 0 0 4 4 6 6 6 9 10 10 10 10
```
* `i.@#-` Subtract that from `0 1 2 ... n`:
```
0 1 2 3 0 1 0 1 2 0 0 1 2 3
```
* `+` Add that to the original input:
```
1 2 3 4 10 11 20 21 22 30 40 41 42 43
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 7 bytes
```
t&=XRs+
```
[Try it online!](https://tio.run/##y00syfn/v0TNNiKoWPv//2hDHQUQMoBhdCIWAA) Or [verify all test cases](https://tio.run/##y00syfmf8L9EzTYiqFj7v0vI/2hDHQUjHQXjWC4QC8IB8ZGFDA1gGJ0AKgEyEIg4LbqmOgoo2ERHARlZAhHCJLirYgE)
### How it works
Consider input `[1, 1, 10, 10, 100, 100, 100, 100]` as an example.
```
t    % Implicit input. Duplicate
     % STACK: [1 1 10 10 100 100 100 100], [1 1 10 10 100 100 100 100]
&=   % Matrix of equality comparisons 
     % STACK: [1 1 10 10 100 100 100 100], [1 1 0 0 0 0 0 0
                                            1 1 0 0 0 0 0 0
                                            0 0 1 1 0 0 0 0
                                            0 0 1 1 0 0 0 0
                                            0 0 0 0 1 1 1 1
                                            0 0 0 0 1 1 1 1
                                            0 0 0 0 1 1 1 1
                                            0 0 0 0 1 1 1 1]
XR   % Upper triangular part, without the diagonal
     % STACK: [1 1 10 10 100 100 100 100], [0 1 0 0 0 0 0 0
                                            0 0 0 0 0 0 0 0
                                            0 0 0 1 0 0 0 0
                                            0 0 0 0 0 0 0 0
                                            0 0 0 0 0 1 1 1
                                            0 0 0 0 0 0 1 1
                                            0 0 0 0 0 0 0 1
                                            0 0 0 0 0 0 0 1]
s    % Sum of each column
     % STACK: [1 1 10 10 100 100 100 100], [0 1 0 1 0 1 2 3]
+    % Add, element-wise. Implicit display
     % STACK: [1 2 10 11 100 101 102 103]
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
ŒɠḶF+
```
[Try it online!](https://tio.run/##y0rNyan8///opJMLHu7Y5qb9/3D70UkPd87QjPz/P5or2lBHwUhHwThWB8yE8EACUDEDHQUEQigyNIBhdAKkRtdURwEFm@goICNLIEIyC25tLFcsAA "Jelly – Try It Online")
## How it works
```
ŒɠḶF+ - Main link. Takes a list L on the left
Œɠ    - Group run lengths
  Ḷ   - Zero based range
   F  - Flatten
    + - Add elementwise to L
```
For example, take `L = [1, 1, 2, 2, 3, 3]`:
* `Œɠ`: `[2, 2, 2]`
* `Ḷ`: `[[0, 1], [0, 1], [0, 1]]`
* `F`: `[0, 1, 0, 1, 0, 1]`
* `+`: `[0, 1, 0, 1, 0, 1] + [1, 1, 2, 2, 3, 3] = [1, 2, 2, 3, 3, 4]`
[Answer]
# [R](https://www.r-project.org/), ~~40~~ 33 bytes
*Edit: -7 bytes thanks to @Giuseppe.*
```
function(a)a+sequence(rle(a)$l)-1
```
[Try it online!](https://tio.run/##ZZDNCsIwDMfvPkVADw220M6peNAXEQ@jVBiMitM9f43Z2tpaktL@88sHGUNP5xzuk7fv/uFFh9325Z6T89aJcXAkbAZUhjlhhZHQSNghrkFd4Lp8b6scnqWv@sctooQ2JWgJ2SKtY5UCnWsbHb2@yl7MmBTlF8t5WLWXUHhL7X7sRBaLxriikVSTWFIPEo7MGl2PmpZRrYtTeW@I4QM "R – Try It Online")
[Answer]
# Haskell + [hgl](https://gitlab.com/WheatWizard/haskell-golfing-library), 11 bytes
```
gr+>zW(+)nn
```
## Explanation
Most of this is pretty simple
* `gr` is "group"; it groups a list into a list of equal segments.
* `zW` is "zip with"; it takes a function and uses it to combine two lists pairwise.
* `(+)` is "plus"; it adds things.
* `nn` is "the natural numbers"; an infinite list of all non-negative numbers in order.
* `zW(+)nn` combines the last 3 into a single thing which takes a list and adds every element with its index.
The last thing here that links `gr` with `zW(+)nn`. The `+>` operator. This operator has a name it's called "Kleisli composition". Regular composition takes functions `a -> b` and `b -> c` and produces a third function `a -> c`, Kleisli composition does very similar. It takes Kleisli morphisms, which are just a particular kind of function, that maps into a monad. So `a -> m b` where `m` is a monad would be an example of a function from `a` to `m b`, but is a Kleisli morphism from `a` to `b`.
So Kleisli composition takes a function `a -> m b` and a function `b -> m c` and produces a function `a -> m c` where `m` is some monad.
This is in fact sort of the fundamental essence of a monad, that Kleisli composition forms a category. So how are we using it here?
Well we have `gr :: List a -> List (List a)` and `zW(+)nn :: List a -> List a` and the thing we want is `List a -> List a`. A naive way to do it would be to group the whole thing map across each element and the concat everything back up:
```
jn<m(zW(+)nn)<gr
```
However an experienced Haskell golfer will notice that map and then concat is exactly the monad behavior of the `List`. So we could potentially get a bind to do the concat and the map in one go.
And now if we look at the types we can see they fit the shape of Kleisli morphisms. So we can actually, compose, map and concat all at once using Kleisli composition.
[Answer]
# Scala, 43 bytes
```
s=>s.indices.map(i=>s(i)+i-s.indexOf(s(i)))
```
[Try it online!](https://scastie.scala-lang.org/sEZcMudCRjSsj3EGXOuJeQ)
A port of [DLosc's great answer](https://codegolf.stackexchange.com/a/240425). Go upvote that!
## My original answer, 48 bytes
```
s=>s.distinct.flatMap(x=>x to x-1+s.count(x.==))
```
[Try it online!](https://scastie.scala-lang.org/7WxItEEnTQODkMZO1M3iaw)
This one gets distinct elements, then for every element `x`, it makes a range from `x` to `x + n`, where `n` is the number of occurrences of `x`.
[Answer]
# [C (tcc)](http://savannah.nongnu.org/projects/tinycc), ~~56~~ 54 bytes
```
c;f(int*p,int*q){for(c=0;p-q;*p+=++c)c*=!(*p-*++p-c);}
```
[Try it online!](https://tio.run/##VVDbboMgGL7nKf65bAHBRVfvqHsRZ4wBTEkcUqW7mPHZHbQaLeHj8MN3AJE4IZZF8BZr42LLwnglU9sPWBQpt8mVx5YWlAoi4uIFxzaJKbWJIHxeXrUR3U0qOI9O6v7j8oXQb68l2MHr1J0eHR71n6oddMow8EWQjWvKisCEwLf7xRZHZUT4veCNYeNoKCDlfjoHul9QuvGO3DfJIGIPYV2tOvOTfPVtgsGMUIjw02iDN6VQcGp0tWhGVWZ55U2njMHe0wc@Dzh55E@Y1/j4fRdLKwaHbXbawh2@J8vZbr8eD8rdBuOfjublHw "C (tcc) – Try It Online")
* -2 bytes thanks to [pxeger](/users/97857/pxeger)
This is a rather straightforward port of my suboptimal [Python answer](https://codegolf.stackexchange.com/a/240429/43394). Takes a pointer to the beginning and end of the array.
## Explanation
```
c;  /* int c; (thanks pxeger) */
f(
    int *p,  /* pointer to the first integer */
    int *q   /* pointer to the last integer */
) {
    for (
        c=0;  /* initialise the increment */
        p-q;  /* reached the end? (if so, stop) */
        /* note: these run out of order */
                         *p+=++c)
        c*=!(*p-*++p-c);
        /*
            equivalent to:
                int original_p0 = p[0] - c;  // undo increment
                if (original_p0 != p[1]) {
                    c = 0;  // different, so reset counter
                }
                p[1] += c;
                c += 1;
                p += 1; // move to next p
        */
}
```
[Answer]
# [R](https://www.r-project.org/), 31 bytes
```
function(a)a+seq(!a)-match(a,a)
```
[Try it online!](https://tio.run/##ZZDNDsIgDMfvPkWNFxpLAnNqPOiLGA8N0biDM@p8fsRugCBpCfz760f69F04e395927o7r1i5OXr/FBzRn3jwV0VE6NQyilL0BCsEBegD3CcvqdZDo/SV/3jJpGgTQmGIFukTaxSoGNta6LXV9lLGJui8hI5D6vXBIW3od2P7YLFojGuw0i6SWxQNwRbYa2pR03LqNYlqbI3RP8B "R – Try It Online")
Port of [Jonah's J answer](https://codegolf.stackexchange.com/a/240439/67312). Uses 1-based indices but it makes no difference.
Test harness taken from [pajonk's answer](https://codegolf.stackexchange.com/a/240433/110034).
[Answer]
# [Husk](https://github.com/barbuz/Husk), 6 bytes
```
ṁGo→Kg
```
[Try it online!](https://tio.run/##yygtzv6f@6ip8f/DnY3u@Y/aJnmn////PzraUMdIxzhWB0iDWEA2mGegA4VQGUMDCELBQDldUx0EMtGBQUsdS5hGiLGxsQA "Husk – Try It Online")
```
     g  # group equal elements
ṁ       # then map the following onto each group
        # (concatenating the results):
 G      #   scan from left
        #   (arg1=result so far, arg2=next element)
  o     #   combination of 2 functions:
    K   #     constant function (= arg1)
   →    #     incremented
```
[Answer]
# [Add++](https://github.com/cairdcoinheringaahing/AddPlusPlus), ~~26~~ 23 bytes
```
L,dBG€bL€RbFz£+dbL1Xz£_
```
[Try it online!](https://tio.run/##S0xJKSj4/99HJ8XJ/VHTmiQfIBGU5FZ1aLF2SpKPYQSQEf9fJScxNyklUcHQzp7L////aF1TBQQyUYBBSwXLWAA "Add++ – Try It Online")
## Explained
```
L,dBG€bL€RbFz£+dbL1Xz£_
L,                       # create a lambda that:
  d                      # pushes two copies of the input
   BG                    # ... groups the second on consecutive items
     €bL                 # ... gets the length of each of those groups
        €R               # ... creates the range [1...n] for each of those lengths (add++ doesn't have a range [0...n) built-in for some reason dang it caird.)
          bF             # ... flattens that list
            z            # and zips it with the original input. This creates a list of items and how much to increment by
             £+          # now, reduce each item by addition
               dbL       # and push the length of the list, keeping a copy on the stack of the original list
                  1X     # push a list of length(^) 1s
                    z£_  # and subtract that from each item in the other list (this is just to account for the fact that there's no [0...n) built-in)
```
[Answer]
# [Prolog (SWI)](http://www.swi-prolog.org), ~~75~~ 66 bytes
```
B+A,[C]-->[A],{C is A+B},B+1+A;[X],{C=X},1+X.
_+_-->[].
a-->0+x,!.
```
[Try it online!](https://tio.run/##KyjKz8lP1y0uz/z/30nbUSfaOVZX1y7aMVan2lkhs1jBUdupVsdJ21Db0To6AiRoG1GrY6gdoccVrx0PUhmrx5UIpA20K3QU9f7/T9SINtRRgCMjIIrVcdLUAwA "Prolog (SWI) – Try It Online")
This program can be run by calling the grammar `a//0` to parse the input list. The remainder after parsing will be unified with the output list.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
+ċṪ$Ƥ
```
A monadic Link accepting a sorted list of integers that yields a list of integers.
**[Try it online!](https://tio.run/##y0rNyan8/1/7SPfDnatUji35//9/tK6pjgIKNtFRQEaWQBQLAA "Jelly – Try It Online")**
### How?
```
+ċṪ$Ƥ - Link: list of integers, A
    Ƥ - for prefixes of A:
   $  -   last two links as a monad, f(prefix):
  Ṫ   -     pop off the tail from the prefix
 ċ    -     count occurrences of that in the remaining prefix
+     - A add that (vectorises)
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~61~~ 60 bytes
I wrote this answer to make sure my algorithm was sound, before I [implemented it in C](https://codegolf.stackexchange.com/a/240436/43394).
```
def f(a,b=.5,c=0):
 for d in a:c*=not d-b;yield d+c;c+=1;b=d
```
[Try it online!](https://tio.run/##VcNLCoAgEADQfaeYpZZF300yJ4kW6SQJoVFuOr0FLSp4bzvD4l0TI80GDJuEwqITGkveJ2D8DgTWwdTrFJ0PQLmSp51XAsq01BlWUiHFbbcusNUegRk2VAJe5bP@bO7t78g5jxc "Python 3 – Try It Online")
## Explanation
```
def f(
    a,     # input list
    b=.5,  # previous value
    c=0    # consecutive counter
):
    for d in a:
        c *= not d-b;  # if d and b are different, c = 0
        yield d + c;
        c += 1;
        b = d          # next loop, this'll be the previous value
```
[Answer]
# [Perl 5](https://www.perl.org/), 37 bytes
```
s/-?\d+/$&-$f?$i=0:$i++;$i+($f=$&)/ge
```
[Try it online!](https://tio.run/##K0gtyjH9/79YX9c@JkVbX0VNVyXNXiXT1sBKJVNb2xpIaKik2aqoaeqnp/7/b6hgpGDMZagAooEsINtAAQrBooYGEISCuXRNFRDIRAEGLRUsIZogxv3LLyjJzM8r/q@bWAAA "Perl 5 – Try It Online")
[Answer]
# [Zsh](https://www.zsh.org/), 38 bytes
```
p=.5
for x;echo $[(c=p-x?0:c+1)+(p=x)]
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m724qjhjwYKlpSVpuhY31Qps9Uy50vKLFCqsU5Mz8hVUojWSbQt0K-wNrJK1DTW1NQpsKzRjIaphmgyiDXQUgEjXFA2b6CggI0swMjSIhWoEAA)
I think there might a solution abusing `mv`'s backup functionality like [this answer](https://codegolf.stackexchange.com/a/35950), but I can't work out how.
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), ~~22 17~~ 10 bytes
Saved 7 bytes thanks to ovs!
```
∊{⊂⍺+⍳≢⍵}⌸
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///qG@qp/@jtgkGXFyPeucqBOTnVKZl5uQopOUXKZTmZRaWpirkJhZnc4UC1VRXG9pq6z/q3Wp7aL3ho7aJQFbtoRU6MSD6fxpQwaOOrupHXU2PendpP@rd/KhzEUjmUc@O///TFAwVjBSMuUA0iAVkg3kGMAiVMTSAIBQMlDu03hQNmyjAoKWCJUw3xGwA "APL (Dyalog Unicode) – Try It Online")
[Answer]
# [Perl 5](https://www.perl.org/) + `-p`, 12 bytes
```
$_+=$h{$_}++
```
[Try it online!](https://tio.run/##K0gtyjH9/18lXttWJaNaJb5WW/v/f11TLgQy4YJBSy7Lf/kFJZn5ecX/dQtyAA "Perl 5 – Try It Online")
[Answer]
# [Python](https://python.org) + [Pandas](https://pandas.pydata.org/), 40 bytes
```
lambda L:S(L)+S(L).groupby(L).cumcount()
```
The code doesn't run on TIO, since it's `pandas`...
But anyway, [here](https://tio.run/##XU7LDoIwELz3KyacaCwGRA@S4Bdw44gcyktJhDZtOfD1SCEomszszkw2uytH8xR9ODVKdJC8r7hG20mhDNJatbXGHKSkie/Ti3dFxZFEqZvQgy3HhxKDLEYry6ErxdAbl06m1kYjRkaQBQwnhjBnq16tTbbQZ/hiNxb4G//LMuRdGH54ZtjjOmO/7XM6JzlphIJ9Em2/dB0RSNXOz1vH4Hg3h6FZHKXTGw) is the code.
[Answer]
# x86-64 machine code, 15 bytes
```
3b 06 74 02 31 c9 ad 29 4e fc ff cf e0 f2 c3
```
[Try it online!](https://tio.run/##VVHLTsMwEDzbX7EYISVpWgpEHFLKj5QcjB@pwXnITquqVX@dsLErWiSPd3Z3xl7ZYl4LMY7cN5BQtqht98ktaEbZR@tUr/hQEtMKUPIQakQ0PSh@yGHjvKli7UuB/zZ9SHRJDp2b9PmfaWqWxHbSy2jgUgb/vKiuKiLVdI@5nNkeIQ4Qc6eQpCu674wEnZh2gDaHKWQcy/c4pN1JBW9@kKZbbN8pnZoNN20Qc1eLHMSWO8gyTPYpPVESOpsK1nB6yuG6lhHPN3hBFP9wXsUDLNq9OapOJzx9vLAwFdGJzSEyfJQwiEH1coXhbg0W42yWQu@woxP2YIChfmOqydLvBp8whvQ8jj9CW177cd68Frjhh63Ro@wv "C (gcc) – Try It Online")
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes the length of the array in RDI and the address of the array of 32-bit integers in RSI. The starting point is after the first 4 bytes.
Assembly:
```
.global f
repeat:
    cmp eax, [rsi]      # Compare last value and current value
    je skip             # Jump if they are equal
f:
    xor ecx, ecx        # (If not equal, or at the start) ECX = 0
skip:
    lodsd               # Put the current value in EAX, and advance the pointer
    sub [rsi-4], ecx    # Subtract ECX from the (same) value
    dec edi             # Count down from the length
    loopnz repeat       # Jump back if not finished, also reducing ECX by 1
    ret                 # Return
```
Here is another solution I found, at 16 bytes, taking the arguments in the opposite order:
```
.global f
f:
    mov eax, edx
repeat:
    cmpxchg [rdi], edx
    cmovne edx, eax
    inc edx
    scasd
    dec esi
    jnz repeat
    ret
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 5 bytes
```
Ġvẏf+
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLEoHbhuo9mKyIsIiIsIlsxLDEsMSwxMCwxMCw0LDQsMiwyLDIsMiw1LDYsNiw2XSJd)
-1 thanks to lyxal
```
Ġ     # Group runs of identical chars
 vẏf  # 0...n each and flatten
    + # Add to input
```
Port of [caird coinheringaahing's answer](https://codegolf.stackexchange.com/a/240422).
[Answer]
# JavaScript (ES6), 51 bytes
```
x=>x.map((y,i)=>y+x.reduce((r,z,k)=>r+=z==y&k<i,0))
```
[Answer]
# APL+WIN, 22 bytes
Prompts for input vector
```
v+¯1++⌿p×+\p←<⍀v∘.=v←⎕
```
[Try it online! Thanks to Dyalog Classic](https://tio.run/##SyzI0U2pTMzJT9dNzkksLs5M/v@ob6qvz6O2CcZcQJanP5BlyPWooz3tf5n2ofWG2tqPevYXHJ6uHVMAlLF51NtQ9qhjhp5tGZAHVP8fqJLrfxqXoYKRgjEXiAaxgGwwz0ABCqEyhgYQhIKBcofWmyqgYhMFGLRUsITphpgNAA "APL (Dyalog Classic) – Try It Online")
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 10 bytes
```
ＩＥθ⁺ι№…θκι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEwzexQKNQRyEgp7RYI1NHwTm/FCRTmZyT6pyRD5bK1tRRyNQEAev//6OjDXUUEMgAgo2QsDEQm6Dg2Nj/umU5AA "Charcoal – Try It Online") Link is to verbose version of code. Does not require the array to be sorted or grouped. Explanation:
```
  θ         Input array
 Ｅ          Map over elements
    ι       Current element
   ⁺        Plus
     №      Count of
         ι  Current element in
      …θκ   Prefix of input
Ｉ           Cast to string
            Implicitly print
```
Alternative approach, also 10 bytes, but requires the array to be grouped:
```
ＩＥθ⁺ι⁻κ⌕θι
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEwzexQKNQRyEgp7RYI1NHwTczD8jI1lFwy8xLAUlkaoKB9f//0dGGOgoIZADBRkjYGIhNUHBs7H/dshwA "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
  θ         Input array
 Ｅ          Map over elements
    ι       Current element
   ⁺        Plus
      κ     Current index
     ⁻      Minus
       ⌕    First index of
         ι  Current element in
        θ   Input array
Ｉ           Cast to string
            Implicitly print
```
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), 68 bytes
```
r(`(?<=(^|,)(\3,)*)((-)?\d+)
#$#2$*1$4$3$*
+`1-1
-
#(-)?(1*)-?
$1$.2
```
[Try it online!](https://tio.run/##VYzBCsJADETv@Y2NkGwTaXbroaDs0Z8oZQU9ePFQPPrva@tWSpkJ80jITI/383UrB7rmMlGmdL7Q@BGmIQp7JlJOw71hcOgCesMOI3posqmBglvuZJ41ARoeQykmQSKYLDnTzK2s@m2trd4N6Ek2d/JXL319qnVf "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation:
```
r(`
```
Use right-to-left matching for the whole script, as that allows the matches to be specified slightly more golfily.
```
(?<=(^|,)(\3,)*)((-)?\d+)
```
Match each integer, and count how many times it's been repeated.
```
#$#2$*1$4$3$*
```
Replace each integer with a marker `#`, the repetition count in unary, and the integer in unary. Note that if the integer is positive this already adds the count to the integer.
```
+`1-1
-
```
If the integer was negative then find the difference between it and the count.
```
#(-)?(1*)-?
$1$.2
```
Convert the integer back to decimal, ignoring a trailing `-` sign, which indicates that the count was at least as large as the absolute value of the integer and therefore the result is no longer negative.
28 bytes in [Retina 1](https://github.com/m-ender/retina/wiki/The-Language) if only non-negative integers need to be supported:
```
r`(?<=(\2,)*)(\d+)
$.(*_$#1*
```
[Try it online!](https://tio.run/##K0otycxLNPyvquGe8L8oQcPexlYjxkhHU0tTIyZFW5NLRU9DK15F2VDr/39DHSMdYy5DHRANZAHZBjpQCBY1NIAgFAyRgegBAA "Retina – Try It Online") Link includes test cases. Explanation:
```
r`(?<=(\2,)*)(\d+)
```
From right to left, match each integer, and count how many times it's been repeated.
```
$.(*_$#1*
```
Add the number of repetitions to the integer.
[Answer]
# [Factor](https://factorcode.org/), 44 bytes
```
[ dup '[ over + swap _ index - ] map-index ]
```
[Try it online!](https://tio.run/##VY/NCsIwEITvfYq5eZBK6x@oDyBevIinUiS0Ww3GNCapWqXPHltrizKzMB/DLmzGEptrt99ttuslzqQlCWS6xIXZEwxdC5IJGRxJkmaCP5nluTRQmqwtlebSYuV5L4QYY4Lqk5rcUMtBr64Ng9b/07T@7MdTdFrU6pe/5yvPRUgLhUGE/EYaQ5g7UziAy5Qe8BHXTyi/pdjNIZlSosTIIBHEtHsD "Factor – Try It Online")
Port of @Dlosc's [Python answer](https://codegolf.stackexchange.com/a/240425/97916).
`dup '[ ... _ ... ]` slots a copy of the input into the quotation at the `_`. `map-index` is like `map` except it also places the index on the stack in addition to the element.
There were a lot of other approaches and attempts; the last three are just longer versions of the above.
```
[ histogram >alist [ first2 over + 1 <range> ] map-concat ]
[ [ ] group-by values [ dup length iota v+ ] map-flat ]
[ histogram [ over + 1 <range> ] f assoc>map concat ]
[ dup '[ _ overd index -rot + - abs ] map-index ]
[| l | l [ | n i | n i + l index - ] map-index ]
[| l | l [ over + swap l index - ] map-index ]
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 5 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
ü ®í+
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=/CCu7Ss&input=WzEsIDEsIDIsIDIsIDMsIDNd)
```
ü      - group
  ®    - for each
   í+  - zip with index then sum
```
[Answer]
# [J-uby](https://github.com/cyoce/J-uby), ~~66~~ 61 bytes
```
:group_by+:next|:values|:*&(:zip%(:size|:*)|:*&:sum)|:flatten
```
[Try it online!](https://tio.run/##y9ItTar8n2b73yq9KL@0ID6pUtsqL7WipMaqLDGnNLW4xkpLTcOqKrNAVcOqOLMqFcjXBIlZFZfmAhlpOYklJal5/wsU0vQ0og11FBDIAIKNkLAxEJug4FjN/wA "J-uby – Try It Online")
~~Surely there is a way to improve this.~~ Yep, `+` is very useful.
[Answer]
# [Ruby](https://www.ruby-lang.org/), 37 bytes
Requires Ruby version 2.7 or newer.
```
->a{a.tally.flat_map{[*_1.._1+_2-1]}}
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m72kqDSpcsGiNNulpSVpuhY3VXXtEqsT9UoSc3Iq9dJyEkvicxMLqqO14g319OINteONdA1ja2uhigMLFNKiow11FBDIAIKNkLAxEJug4NhYLiSdRmBkDESxsRCDFyyA0AA)
##### A quick rundown:
`->a{X}` is an anonymous lambda function taking a parameter `a` and returning `X`.
`tally` returns a hash with each unique number in the input list and the number of times that number appeared in the list.
`[*1..4]` returns `[1,2,3,4]`.
`flat_map` returns a flattened list from the result of the given `{code block}` being run on each number/count pair returned by `tally`, where `_1` is a placeholder for the number and `_2` is the count. So for example with an array with 5 `2`s, `[*_1.._1+_2-1]` returns `[2,3,4,5,6]`.
]