Given a list with repeated elements, for example:
list = {m, i, s, s, i, s, s, i, p, p, i}
I'd like to determine, for each position in the list, how many times the element at that position has appeared in the list up to and including that position.
That is, I'd like to define a function f
such that, for my example list
above,
f[list] = {1, 1, 1, 2, 2, 3, 4, 3, 1, 2, 4}
Notice that
f[list]〚1〛= 1
because position 1 is the firstm
in the listf[list]〚5〛= 2
because position 5 is the secondi
in the listf[list]〚7〛= 4
because position 7 is the fourths
in the listf[list]〚9〛= 1
because position 9 is the firstp
in the list- etc.
This seems like the kind of thing there could well be a built-in for already, but I wouldn't have any guesses as to what it would be called or how to search for it. If not, is there a simple way to implement it? (Probably involving MapIndexed
, I'd guess.)
Table[Count[Take[list, j], list[[j]]], {j, 1, Length@list}]
$\endgroup$