# Enumerate each series of identical numbers in-place

Given a list of strictly positive integers, go through each distinct number and replace all occurrences of it with successive indices (zero or one based) of a new series.

## Examples

[][]/[]

[42][0]/[1]

[7,7,7][0,1,2]/[1,2,3]

[10,20,30][0,0,0]/[1,1,1]

[5,12,10,12,12,10][0,0,0,1,2,1]/[1,1,1,2,3,2]

[2,7,1,8,2,8,1,8,2,8][0,0,0,0,1,1,1,2,2,3]/[1,1,1,1,2,2,2,3,3,4]

[3,1,4,1,5,9,2,6,5,3,5,9][0,0,0,1,0,0,0,0,1,1,2,1]/[1,1,1,2,1,1,1,1,2,2,3,2]

• So basically the number of times it has appeared the sequence so far?
– Jo King
Commented Jan 8, 2019 at 9:41
• @JoKing Yes, that's another way to state it, but "so far" implies zero-based, and "until and including this" implies one-based. I wanted to keep the choice.
Commented Jan 8, 2019 at 9:42

# PowerShell Core, 25 bytes

$t=@{}$args|%{(++$t.$_)}

Try it online!

# Wolfram Language (Mathematica), 21 bytes (@att)

_g=g/@#-(--g[#]&/@#)&

Try it online!

## Wolfram Language (Mathematica), 28 bytes

(Clear@g;g@a_=0;++g[#]&/@#)&

Based on @att's answer to another challenge.

Try it online!

## Wolfram Language (Mathematica), 32 bytes

-1 byte thanks to @att.

Accumulate[p=x^#]~Coefficient~p&

The $$\k\$$-th element in the answer is the coefficient of the $$\x^{a_k}\$$ term in the polynomial $$\\sum_{i=1}^kx^{a_i}\$$.

Try it online!

• -1 byte on the first, and -2 bytes on the second
– att
Commented Jan 3, 2022 at 19:23
• Roman had a good insight that saves -2 bytes on the second, and probably all other answers that used Clear as well.
– att
Commented Jan 25, 2022 at 20:23
• I just realized that this specific golf isn't actually valid here (nor on your AoCG17; I'm not sure about your AoCG16), since the function won't be reusable on the same input. Sorry about that, I jumped the gun.
– att
Commented Jan 27, 2022 at 2:33
• 24 on the second
– att
Commented Feb 20, 2022 at 8:44
• _g=0Clear@g could've saved 1 otherwise
– att
Commented Feb 20, 2022 at 8:46

# Retina 0.8.2, 30 bytes

\b(\d+)\b(?<=(\b\1\b.*?)+)
$#2 Try it online! Link includes test cases. 1-indexed. Explanation: The first part of the regex matches each integer in the list in turn. The lookbehind's group matches each occurrence of that integer on that line up to and including the current integer. The integer is then substituted with the number of matches. ## Batch, 61 bytes @setlocal @for %%n in (%*)do @set/ac=c%%n+=1&call echo %%c%% 1-indexed. Because variable substitution happens before parsing, the set/a command ends up incrementing the variable name given by concatenating the letter c with the integer from the list (numeric variables default to zero in Batch). The result is then copied to another integer for ease of output (more precisely, it saves a byte). # Japt, 8 bytes £¯YÄ è¶X Try it here £¯YÄ è¶X :Implicit input of array U £ :Map each X at 0-based index Y ¯ : Slice U to index YÄ : Y+1 è : Count the elements ¶X : Equal to X # Haxe, 58 bytes l->{var x=[0=>0];l.map(e->++x[(x[e]==null?x[e]=0:0)+e]);}; (Requires arrow functions, so 4.0+) var x=[0=>0] declares a new IntMap, with 0 as its only key (since the question say inputs are strictly positive). Unfortunately most targets throw when adding null to a number, hence the explicit check to make sure each key is in the map before incrementing. Also a cheeky rip off based on the JS answer: # Haxe (JS target), 41 bytes l->{var x=[0=>0];l.map(e->x[e]=-~x[e]);}; Try both online GO 121 Bytes (Not included new lines and tabs) func cg(a []int) []int{ var m = make(map[int]int) var r = make([]int, len(a)) for i,v := range a{ r[i] = m[v] m[v]++ } return r } Accepts integer array and returns integer array. • Welcome to PPCG. Rare to see Go represented here. You do need to count necessary newlines, but you also have a lot of spaces that can be removed, and you can also shorten cg to c. I recommend using TIO to generate the main part of your post: Try it online! – Adám Commented Jan 9, 2019 at 9:53 # Python 2, 44 bytes a=[] for x in input():print a.count(x);a+=x, Try it online! The first thing I wrote tied Chas Brown's 43, so here's a different solution that's one byte longer. # Reticular, 17 bytes L[ddc@c~]~*$qlbo;

Try it online!

The input list of integers is assumed to already be pushed to the stack. Run the following code to test input:

'2''7''1''8''2''8''1''8''2''8'lbL[ddc@c~]~*$qlbo; ## Explanation The pop instruction c which is supposed to: pop a list from the stack, pop the last element from that list and push this element to the stack. However, if the list occurs at more than 1 place in the stack (if it has been duplicated for example), all of the duplicated lists in the stack will also have their last element popped contrary to what one might think will happen. This is fortunately used in our favor in this puzzle. L # Push length of input list to the stack. [ ] # Push the following function: dd # Duplicate top of stack twice. c # Pop the list at top of the stack, pop the last element in the list (which will pop the element of every list!) and finally push it to the stack. @c # Pop two items at the top of the stack (list + last element in that list). then push the number of occurrences of that element in the list. ~ # Swap top two items in the stack (so that the popped list is on top again). ~ # Swap top two items in the stack. * # Call the above function the same number of times as length of the input list.$      # Remove the item at the top of the stack (which by now is an empty list).
q     # Reverse stack.
lb   # Push size of stack and put that many items from the stack into a list.
o; # Output resulting list and exit.

# SNOBOL4 (CSNOBOL4), 63 bytes

T =TABLE()
R I =INPUT :F(END)
T<I> =OUTPUT =T<I> + 1 :(R)
END

Try it online!

# awk 46 31 bytes

-15 bytes thanks to @Dennis

{for(;i++<NF;)$i=a[$i]++;print}

TIO

• I think {for(;i++<NF;)$i=a[$i]++;print} works. delete(a); is only needed for the test suite, no? Commented Jan 11, 2019 at 1:10
• @Dennis, thank you for your feedback, however DigitalTrauma's version is still shorter Commented Jan 11, 2019 at 8:10

# C# (.NET Core), 172 bytes

Without LINQ. 0 indexed int[] arrays.

p=>{int h=0,m=0,l=p.Length,j;var k=new int[l];if(l>0){foreach(int z in p){h=z>h?z:h;m=z<m?z:m;}for(j=m;j<=h;j++,m=0)for(int z=0;z<l;z++)k[z]=p[z]==j?m++:k[z];}return k;}

Try it online!

# Pyth, 7 bytes

.e/<Qkb

Try it online!

.e     Q # enumerated map over (implicit) Q (input): lambda k,b: (k=index, b=element)
/   b  # number of occurences of b in
<Qk   # Q[:k]

## Alternative 1-based solution (7 bytes)

m/ded._

Try it online!

m    ._ # map over all prefixes of (implicit) Q (input): lambda d:
/ ed   # Number of occurences of d[-1] in
d     #                                  d

# SAP ABAP, 174 bytes

FORM f TABLES t.DATA r TYPE int_tab1.LOOP AT t.DATA(i) = 0.DATA(x) = sy-tabix.LOOP AT t.CHECK:sy-tabix < x,t = t[ x ].i = i + 1.ENDLOOP.APPEND i TO r.ENDLOOP.t[] = r.ENDFORM.

Subroutine which takes a table of integers and replaces all values with zero-based indices. ABAP has no array type, so a table of integers is the closest thing we have. It uses the builtin table type int_tab1 to save some bytes over declaring TYPE TABLE OF i and abuses obsolete (but still functional) internal tables with header lines, which allows us to use LOOP AT t and read t at the implicit index sy-tabix without declaring additional variables for keeping the value.

Full program with some test cases from OP can be found here. Output below:

Forgot to add the two edge cases of [ ] and [42], but they work, too:
[ ] => An empty input table stays empty as the outer loop gets skipped completely
[42] => A single value in the table means table line 1 in the inner loop is skipped, since sy-tabix is equal to x - therefore i stays 0 and the output is [0].

### Explanation:

FORM f TABLES t.              "Subroutine, TABLES parameters are references
DATA r TYPE int_tab1.       "A temporary internal table for results
LOOP AT t.                  "Go over each line in table t
DATA(i) = 0.              "Set count to 0            (inline declaration)
DATA(x) = sy-tabix.       "Set x to current index    (inline declaration)
LOOP AT t.                "Go over each line in table t again
CHECK:                  "Check whether... (works like a CONTINUE if not true)
sy-tabix < x,         "1) ...inner LOOP index is smaller than outer index (= only check lines up to outer index - 1)
t = t[ x ].           "2) ...value of t at inner index (implicit) is equal to value of t at outer index (x)
i = i + 1.              "If both are true, increase the counter by 1
ENDLOOP.
APPEND i TO r.            "Add the count for this value to temporary results table
ENDLOOP.
t[] = r.                    "Fill referenced table t with the values in r.
ENDFORM.

# Gema, 32 characters

<D>=@set{$1;@add{${$1;};1}}${$1} Outputs 1 based indexes. Sample run: bash-5.0$ gema '<D>=@set{$1;@add{${$1;};1}}${$1}' <<< '[5,12,10,12,12,10]' [1,1,1,2,3,2] Try it online! ## Gema, 34 characters <D>=${$1;0}@set{$1;@add{${$1;};1}}

Outputs 0 based indexes.

Sample run:

bash-5.0$gema '<D>=${$1;0}@set{$1;@add{${$1;};1}}' <<< '[5,12,10,12,12,10]'
[0,0,0,1,2,1]

Try it online!

# C++ (gcc), 122 bytes

#import<vector>
using V=std::vector<int>;V f(V x){for(int i,t,z=x.size();t=i=z--;x[z]=t)for(;i--;)t-=x[z]!=x[i];return x;}

Try it online!

Saved 2 more bytes thanks to @ceilingcat

# Go, 85 bytes

package main
func main(){
m:=make(map[int]int)
for i,n:=range a{
a[i]=m[n]
m[n]++
}
}

Create a map to store the count, loop through input list (a), replacing each item with the count for that item before incrementing.

(+/⊃⍷⊢)⍤⌽¨,\⎕

# Thunno 2, 4 bytes

ƒıṪc

Try it online!

#### Explanation

ƒıṪc­⁡​‎‎  # Implicit input⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌⁢​‎‎⁪⁡⁪⁠⁪⁢⁪‏⁠⁪⁪‏​⁡⁠⁡‌⁣​‎‎⁪⁡⁪⁠⁪⁣⁪‏‏​⁡⁠⁡‌⁤​‎‎⁪⁡⁪⁠⁪⁤⁪‏‏​⁡⁠⁡‌⁢⁡​‎‎⁪⁡⁪⁠⁪⁡⁪‏‏​⁡⁠⁡‌­
ƒ     # ‎⁡Get the prefixes of the input list
ı    # ‎⁢Map over this list of lists:
Ṫ   # ‎⁣ Remove the last item and push it separately
c  # ‎⁤ Count the occurrences of this letter
# ‎⁢⁡Implicit output