Skip to content

Commit

Permalink
Merge pull request #2627 from rmrt1n/287
Browse files Browse the repository at this point in the history
Create 0287-find-the-duplicate-number.rs
  • Loading branch information
tahsintunan committed Jun 29, 2023
2 parents 569fd8a + 7264d28 commit 84fa4cb
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions rust/0287-find-the-duplicate-number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
impl Solution {
pub fn find_duplicate(nums: Vec<i32>) -> i32 {
let (mut slow, mut fast) = (nums[0], nums[nums[0] as usize]);
while slow != fast {
slow = nums[slow as usize];
fast = nums[nums[fast as usize] as usize];
}

slow = 0;
while slow != fast {
slow = nums[slow as usize];
fast = nums[fast as usize];
}

slow
}
}

0 comments on commit 84fa4cb

Please sign in to comment.