Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 2, 2026 17:21
Show Gist options
  • Select an option

  • Save thinkphp/0af5b1beb117cbe3ab491afc7884f365 to your computer and use it in GitHub Desktop.

Select an option

Save thinkphp/0af5b1beb117cbe3ab491afc7884f365 to your computer and use it in GitHub Desktop.
floyd-cycle-detection.java
//Floyd's Algorithms Cycle detection - find duplicate Complexity O(n)
class Solution {
public int findDuplicate(int[] nums) {
int slow = nums[0];
int fast = nums[0];
do {
slow = nums[slow];
fast = nums[ nums[ fast ] ]
} while( slow != fast)l
fast = nums[0];
while(slow != fast) {
slow = nums[slow];
fast = nums[fast];
}
return fast;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment