Simple java solution


  • -3

    public int findDuplicate(int[] nums) {

        Set<Integer> set = new HashSet<Integer>();
        for(int i=0;i<nums.length;i++){
            if(!set.add(nums[i])){
                return nums[i];
            }
        }
        return -1;
    }

  • 0
    R

    You must use constant space


Log in to reply
 

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