Skip to main content
1 of 3
Maël
  • 419
  • 2
  • 9

R, 58 bytes

function(v,x){d=diff(c(0,which(v==x)))
sequence(d,d-1,-1)}

output

f(int, 0)
# [1] 2 1 0 2 1 0 0
f(int, 1)
# [1] 0 0
f(int, 2)
# [1] 4 3 2 1 0 2 1 0
f(int, 3)
# integer(0)

f(char, "A")
# [1] 0 2 1 0 0
f(char, "B")
# [1] 1 0
f(char, "C")
# [1] 2 1 0
f(char, "D")
# integer(0)

Works for R >= 4.0.0.

Maël
  • 419
  • 2
  • 9