Files
codewars/cycle-detection-floyds-the-tortoise-and-the-the-hare/index.js
2019-11-23 23:45:30 +01:00

15 lines
286 B
JavaScript

// https://www.codewars.com/kata/cycle-detection-floyds-the-tortoise-and-the-the-hare/javascript
const floyd = function(f, x0) {
let x = x0;
let i = 0;
const map = new WeakMap();
while (!(x in map)) {
map[x] = i++;
x = f(x);
}
return [map[x], i - map[x]];
}