Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int findDuplicate(int[] nums) {
- if (nums.length > 1){
- int ptr1 = nums[0];
- int ptr2 = nums[nums[0]];
- while (ptr1 != ptr2)
- {
- ptr1 = nums[ptr1];
- ptr2 = nums[nums[ptr2]];
- }
- ptr2 = 0;
- while (ptr2 != ptr1)
- {
- ptr2 = nums[ptr2];
- ptr1 = nums[ptr1];
- }
- return ptr1;
- }
- return -1;
- }
Add Comment
Please, Sign In to add comment