Skip to main content
2 of 3
added 27 characters in body
Maël
  • 419
  • 2
  • 9

R, 56 bytes

-2 bytes thanks to Giuseppe.

function(v,x) sequence(d<-diff(c(0,which(v==x))),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