I just share my solution,it is very simple.but it sort the array


  • 0
    L
      public int findDuplicate(int[] nums) {
          
        if(nums == null || nums.length == 0){
               return 0;
           }   
         Arrays.sort(nums);
        if(nums.length == 1) return 1 - nums[0];
        for(int i = 0; i < nums.length - 1; i++){
            if(nums[i] == nums[i+1]) return nums[i];
        }
        return 0;
    }

Log in to reply
 

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