Find duplicate


  • 0
    S

    This question states that there is just one duplicate. However, my following solution fails for the test [2,2,2,2,2]. This test case contains more than one duplicates, how is this possible?
    public static int findDuplicate(int[] nums) {
    if(nums.length==0)
    return 0;
    if(nums.length==2)
    return nums[0]==nums[1]?nums[0]:-1;
    int len=nums.length-1;
    int osum=(len*(len+1))/2;
    int gsum=0;
    for(int i:nums)
    gsum+=i;
    return osum-gsum;
    }


  • 0

    Use a dictionary....


Log in to reply
 

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