Very Easy Java Code, 5 lines. o(N) without extra space.


  • 0
    W
    public int findDuplicate(int[] nums) {
            for (int i = 0; i < nums.length; i++) {
                int abs = Math.abs(nums[i]);
                if (nums[abs] < 0) {
                    return abs;
                }
                nums[abs] = -nums[abs];
            }
            return 0;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.