Skip to content

Instantly share code, notes, and snippets.

@webber2408
Created October 7, 2022 20:32
Show Gist options
  • Save webber2408/e12ab892dff4c309be43beaeed591875 to your computer and use it in GitHub Desktop.
Save webber2408/e12ab892dff4c309be43beaeed591875 to your computer and use it in GitHub Desktop.
Approach-6) Find Duplicates - Floyd's Hare & Tortoise Algorithm
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int tort = nums[0], hare = nums[0];
do{
tort = nums[tort];
hare = nums[nums[hare]];
}while(tort != hare);
tort = nums[0];
while(tort != hare){
tort = nums[tort];
hare = nums[hare];
}
return tort;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment