My Java Solution


  • 0
    R
    public class Solution {
        public int findDuplicate(int[] nums) {
            int answer = -1;
            for(int i = 0; i < nums.length; i++){
                int value = Math.abs(nums[i]) - 1;
                if(nums[value] > 0){
                    nums[value] = -nums[value];
                }
                else{
                    answer = nums[i];
                }
            }
            return Math.abs(answer);
        }
    }

  • 0
    V

    @rahil3 cool. but you modified the array


Log in to reply
 

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