How come it accepted this solution?

  • 1
    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++){
                    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

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

  • 1

    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.