How come it accepted this solution?


  • 1
    G
    public class Solution {
        public int findDuplicate(int[] nums) {
        int l = nums.length;
        for(int i=0;i<l;i++){
            for(int j=i+1;j<l;j++){
                if(nums[i]==nums[j]){
                    return nums[i];
                }
             }
          }
        return -1;
       }
    }
    

    I just gave it a shot and I thought it would give time limit exceeded error..but it accepted ;-)


  • 0
    R

    Yeah... I was trying to wrap my head around this too, so I started with this naive solution and it worked...


  • 1
    S

    I use Arrays.sort(nums). This is O(nlogn) and do one for loop to find the duplicate...which is accepted as well...


Log in to reply
 

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