A easy way use the array itself


  • 0
    A

    '''
    public class Solution {
    public int FindDuplicate(int[] nums) {
    for(int i = 0;i<nums.Length;i++){
    int val = nums[i];
    int index = Math.Abs(val)-1;
    if(nums[index]<0){
    return index+1;
    }else{
    nums[index] = -nums[index];
    }
    }
    return -1;
    }
    }
    '''


Log in to reply
 

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