Find Duplicate Number in Array in Java



Given an array of size n + 1 with elements from 1 to n. One element is duplicated multiple times. Find that element in O(1) space. An array cannot be changed.
public class DuplicateNumberDetection {
    public int findDuplicate(int[] nums) {
        if (nums.length == 0 || nums.length == 1) {
            return -1;
        }

        int slow = nums[0];
        int fast = nums[nums[0]];
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        fast = 0;
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[fast];
        }

        return fast;
    }

    public static void main(String args[]) {
        int[] input = {2,1,3,1,5,4};
        DuplicateNumberDetection dd = new DuplicateNumberDetection();
        System.out.println(dd.findDuplicate(input));
    }
}
Output:
1

Comments