4 of 5 added 2 characters in body

J, 29 23 bytes


Try it online!

The real work is done in the iteration verb of the power verb ^:, which iterates as many times as the argument [, starting the iteration with the constant value 0 &0...

  • (#|1+}.i.{.) This is what iterates. Breaking it down...
  • }.i.{. Find the index of i. of the head of the list {. within the tail of the list }.. This will return a 0-based index, so if the current item is found 1 previous it will return 0. If it is not found, it will return the length of the list, ie, the length of the tail.
  • 1+ Add one to the value to correct for the 0-based indexing, since the Ven Eck's "how far back" is 1-based. Note that if it was not found, the value will now be the length of the full list.
  • #| Return the remainder of the value calculated in the previous step, when divided by the length of the full list. Note that this turns "not found" into 0, but leaves all other values unchanged.
  • ,~ Append the new value to the front of the list. We use the front rather than make merely for convenience.
  • 1{ return the 2nd item in the list, since we calculated one too many times because it's shorter that way.